C Language | Structure and Union: Nested structures

Nested structures (structure within a structure)

Question:1.
C program to declare a structure named student with attributes as roll, name, total and per. Declare another structure named marks within the structure student with attributes as m1,m2 and m3. Take input for the details and display them.
Sol:

#include<stdio.h>
#include<conio.h>
struct student
{
  int roll;
  char name[20];
  struct marks
  {
    float m1,m2,m3;
  }m;
  float total,per;
}s;
//or
/* 2nd method to declare */
/*
struct marks
{
  float m1,m2,m3;
};
struct student
{
  int roll;
  char name[20];
  struct marks m;
  float total,per;
}s;
*/
//struct student s;
int main()
{
  //clrscr();
  //struct student s;
  printf("Enter roll and name ");
  scanf("%d %s",&s.roll,s.name);
  printf("Enter marks of m1,m2 and m3 ");
  scanf("%f %f %f",&s.m.m1,&s.m.m2,&s.m.m3);
  s.total=s.m.m1+s.m.m2+s.m.m3;
  s.per=s.total/3;
  printf("Total = %f per = %f\n",s.total,s.per);
  getch();
  return(0);
}
/* Output */
Enter roll and name 101
Amita
Enter marks of m1,m2 and m3 98
95
97
Total = 290.000000 per = 96.666664

Question:2
C program to declare a structure name employee with attributes as empno, name and sal. Declare another structure named address within the structure employee with attributes as hno,street,city and state. Take input for the details and display them.
Sol:

#include<stdio.h>
#include<conio.h>
struct employee
{
  int empno;
  char name[20];
  float sal;
  struct address
  {
    char hno[5],street[10],city[15],state[15];
  }ad;
}e;
int main()
{
  //clrscr();
  printf("Enter empno,name and sal ");
  scanf("%d %s %f",&e.empno,e.name,&e.sal);
  printf("Enter hno,street,city and state ");
  scanf("%s %s %s %s",e.ad.hno,e.ad.street,e.ad.city,
			e.ad.state);
  printf("empno %d name %s sal %f\n",e.empno,e.name,
					e.sal);
  printf("hno %s street %s city %s state %s\n",
  e.ad.hno, e.ad.street,e.ad.city,e.ad.state);
  getch();
  return(0);
}
/* Output */
Enter empno,name and sal 1001 Sumit 95000
Enter hno,street,city and state A1
Bada
Gwalior
MP
empno 1001 name Sumit sal 95000.000000
hno A1 street Bada city Gwalior state MP

Question:3
C program to declare a structure named employee with attributes as empno,name,basic salary and net salary. Declare another structure named benefit within employee with attributes as hra,da,ta and cca. Declare another structure named deduction within employee with attributes as pf and it. Take input for the details and display the details along with net salary.
Sol:

#include<stdio.h>
#include<conio.h>
struct employee
{
  int empno;
  char name[20];
  float bsal;
  struct benefit
  {
    float hra,da,ta,cca;
  }b;
  struct deduction
  {
    float pf,it;
  }d;
  float net;
}e;
int main()
{
  //clrscr();
  printf("Enter empno,name and basic salary ");
  scanf("%d %s %f",&e.empno,e.name,&e.bsal);
  //to cal benefit
  e.b.hra=.15*e.bsal;
  e.b.da=.98*e.bsal;
  e.b.cca=.14*e.bsal;
  e.b.ta=.15*e.bsal;
  //to cal deduction
  e.d.pf=.10*e.bsal;
  e.d.it=.10*e.bsal;
  //to cal net sal
  e.net=e.bsal+e.b.hra+e.b.ta+e.b.da+e.b.cca-e.d.pf-e.d.it;
  printf("Net salary = %f\n",e.net);
  getch();
  return(0);
}

/* Output */
Enter empno,name and basic salary 1001
Komal
45000
Net salary = 99900.000000

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects