Skip to main content

Featured

Data of Employees using Structure

There is a structure called employee that holds information like employee code, name, date of joining. That is a program that creates an array of the structure and enters some data. Then ask the user to enter current date. After that Display the names of those employees whose tenure is 3 or more than 3 years according to the given current date. #include<stdio.h> #include<stdlib.h> struct emp{     int code,date;     char name[16]; }; int main() {      struct emp e[20];      int x,i,j,cdate,res;      printf("How many employee are there?");      scanf("%d",&x);      for(i=0;i<x;i++)      {          printf("Enter Employee code:");//int          scanf("%d",&e[i].code);          printf("Enter employee name:");          scanf("%s",e[i].name);          printf("Enter date of joining(yyyymmdd):");          scanf("%d",&e[i].date);      }      printf("Enter current date(yyyymmdd):")

Arrays In C

An array is a variable that can store multiple values. Like you can store your name as a string and you can also store integer values, float values etc. For example you can store many numbers of integer in a single array. You don't have to make 10 or 100 or many scanfs in your code.


Here is the simple example of an Array:-)


//Example of 1D Array

#include<stdio.h>

int main()

{

    int arr;

    char str[10];

    printf("Enter your Name: ");

    scanf("%s",&str);

    printf("Enter your roll no: ");

    scanf("%d",&arr);

    printf("Hello %s and %d is your roll no.",str,arr);

    int n,i,physum=0,matsum=0,chemsum=0;

    printf("\nEnter the number of students in your class: ");

    scanf("%d",&n);

    int maths[n],chem[n],phy[n];

    printf("Enter marks of Maths for\n");

    for(i=1;i<=n;i++)

    {

        printf("\nStudent %d: ",i);

        scanf("%d",&maths[i]);

    }

     printf("Enter marks of Physics for\n");

    for(i=1;i<=n;i++)

    {

        printf("\nStudent %d: ",i);

        scanf("%d",&phy[i]);

    }

     printf("Enter marks of Chemistry for\n");

    for(i=1;i<=n;i++)

    {

        printf("\nStudent %d: ",i);

        scanf("%d",&chem[i]);

    }

    for(i=1;i<=n;i++)

    {

        physum+=phy[i];

        matsum+=maths[i];

        chemsum+=chem[i];

    }

    printf("\nSum of all the marks obtained by %d students in\nMaths:%d\nPhysics:%d\nChemistry:%d",n,matsum,physum,chemsum);

    printf("\n");

    for(i=1;i<=n;i++)

    {

        printf("\nAverage of all marks for student %d is:%d",i,(phy[i]+maths[i]+chem[i])/3);

    }

    printf("\nAverage marks in Maths: %d",matsum/n);

    printf("\nAverage marks in Physics: %d",physum/n);

    printf("\nAverage marks in Chemistry: %d",chemsum/n);


    return 0;

}

//Example of 2D Array

#include<stdio.h>

int main()

{

    char name_of_students[6][8]={"Govind","Yesha","Mark","Tina","Porter"};

    for(int i=0;i<6;i++)

    {

        for(int j=0;j<8;j++)

        {

             printf("%c",name_of_students[i][j]);

        }printf("\n");

    }


    return 0;

}


Popular Posts