5 Computer -- Programming II

Why do we use structure? Write a C program using structure to input name, age and salary of few employees and display it.

Why do we use structure? Write a C program using structure to input name, age and salary of few employees and display it.


Structures in C are used to group together related data of different data types into a single user-defined data type. They provide a way to organize and represent complex data structures in a more manageable way.



OUPUT:

Enter the number of records u wanted to get 3

Enter the name, age and salary

Aadarsh 17 92382

Enter the name, age and salary

Bhagat 17 2872302

Enter the name, age and salary

Vision 17 8293202

Name is Aadarsh

 Age is 17

 Salary is 92382

Name is Bhagat

 Age is 17

 Salary is 2872302

Name is Vision

 Age is 17

 Salary is 8293202



PROGRAMS:

#include<stdio.h>

struct records{

    char name[30];

    int age;

    int salary;

}s[200];

int main()

{

    int n;

    printf("Enter the number of records u wanted to get ");

    scanf("%d",&n);

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

    {

        printf("Enter the name, age and salary\n");

        scanf("%s %d %d",s[i].name,&s[i].age,&s[i].salary);

    }

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

    {

        printf("Name is %s\n Age is %d\n Salary is %d\n",s[i].name,s[i].age,s[i].salary);

    }

}

More questions on Programming II

Differentiate between structure and union.

Structures in Cis a user-defined data type available in C that allows to combining of data items of different kinds. Structures are used to represent a record.

Defining a structure:To define astructure, you must use thestructstatement. The struct statement defines a newdata type, with more than or equal to one member. The format of the struct statement is as follows:

struct [structure name] { member definition; member definition; ... member definition; }; (OR) struct [structure name] { member...
Close Open App