Programs On Structures

Structures – Theory and Programs [Learn to code]

By February 5, 2018 No Comments

What are Structures? Learn syntax for Structures and different programs on Structures.

  • 095. Define a structure Employee having elements emp_id, name,etc. Accept data and reprint it
    #include<stdio.h>
    #include<conio.h>  
    
    struct Employee
    {
     char name;
     int emp_id;
     long phone_no;
    };
    
    main()
    {
    	struct Employee e;
    	clrscr();
    	printf("Enter name : ");
    	scanf("%s",&e.name);
    	printf("Enter emp_id: ");
    	scanf("%d",&e.emp_id);
    	printf("Enter Phone Number: ");
    	scanf("%ld",&e.phone_no);
    
    	printf("\n\nEnter name : %s",e.name);
    	printf("\n\nEnter Emp Id       : %d",e.emp_id);
    	printf("\n\nEnter Phone Number : %ld ",e.phone_no);
    
    	getch();
    }
    
    
    
  • 096. Define a structure Student having fields roll_no, name, marks, etc, for 5 students, accept data and reprint
    
    #include<stdio.h>
    #include<conio.h>  
    
    struct Student
    {
     char name;
     int roll_no;
     int m1,m2,m3;
    };
    
    main()
    {   int i;
    	struct Student s;
    	clrscr();
    	for(i=0;i<5;i++)
    	{   printf("\nEnter data for Student %d.....\n",i+1);
    		printf("Enter name           : ");
    		scanf("%s",&s.name);
    		printf("Enter Roll No.       : ");
    		scanf("%d",&s.roll_no);
    		printf("Enter marks for sub1 : ");
    		scanf("%d",&s.m1);
    		printf("Enter marks for sub2 : ");
    		scanf("%d",&s.m2);
    		printf("Enter marks for sub3 : ");
    		scanf("%d",&s.m3);
    	}
    
    	for(i=0;i<5;i++)
    	{   printf("\nStudent   %d\n",i+1);
    		printf("Name    : %s\n",s.name);
    		printf("Roll No.: %d\n",s.roll_no);
    		printf("Sub1    : %d\n",s.m1);
    		printf("Sub2    : %d\n",s.m2);
    		printf("Sub3    : %d\n",s.m3);
    	}
    
    	getch();
    }
    
    
    
  • 097. Define a structure Employee having elements emp_id, name, DOB, DOJ etc. Accept data and reprint it. (use structure within structure)
    #include<stdio.h>
    #include<conio.h>  
    
    struct Date
    {
    	int mm,dd,yy;
    };
    
    struct Employee
    {
    char name;
     int emp_id;
     struct Date DOB,DOJ;
    };
    
    main()
    {   int i;
    	struct Employee e;
    	clrscr();
    		printf("\nEnter name           : ");
    		scanf("%s",&e.name);
    		printf("\nEnter emp_id.        : ");
    		scanf("%d",&e.emp_id);
    		printf("\nEnter Date of Joining\n ");
    		printf("(dd-mm-yy) : ");
    		scanf("%d-%d-%d", &e.DOJ.dd,&e.DOJ.mm,&e.DOJ.yy);
    		printf("\nEnter Date of birth\n ");
    		printf("(dd-mm-yy) : ");
    		scanf("%d-%d-%d", &e.DOB.dd,&e.DOB.mm,&e.DOB.yy);
    
    		printf("\nName           : %s",e.name);
    		printf("\nEmployee ID    : %d",e.emp_id);
    		printf("\nEnter DOJ      : %d-%d-%d", e.DOJ.dd,e.DOJ.mm,e.DOJ.yy);
    		printf("\nEnter DOB      : %d-%d-%d", e.DOB.dd,e.DOB.mm,e.DOB.yy);
    
    	getch();
    }
    
    
    

Leave a Reply

DEMO01