Structure Programs
Structure Programs
QUESTION NO 1
Write a program that declares a structure Student to
store Roll No (int), name (string) and marks of 05
subject (array of integer with const SIZE), average
(float) marks of a student. The program should define
a structure variable, inputs the values, finds the
average and then displays the student
values on the screen.
#include <iostream>
struct Student
int marks[SIZE];
string name;
int roll_no;
float avg;
};
int main()
{
Student s;
cout<<"Enter Name"<<endl;
cin>>s.name;
cin>>s.roll_no;
int sum=0;
for(int i=0;i<SIZE;i++)
cin>>s.marks[i];
sum=sum+s.marks[i];
s.avg=sum/SIZE;
cout<<"Name is : "<<s.name<<endl;
for(int i=0;i<SIZE;i++)
return 0;
QUESTION NO 1
#include <iostream>
struct Struct
int no;
};
int main()
Struct s;
cin>>s.no;
cout<<"No is : "<<s.no<<endl;
return 0;
QUESTION NO 2
#include <iostream>
struct person
int id;
string name;
int age;
};
int main()
{
person p;
cin>>p.name;
cin>>p.age;
cout<<"Enter ID "<<endl;
cin>>p.id;
cout<<"Name is : "<<p.name<<endl;
return 0;
QUESTION NO 3
#include <iostream>
struct Student
int roll_no;
int marks_e,marks_p,marks_c;
float avg;
};
int main()
int sum;
Student s;
cin>>s.marks_e;
cin>>s.marks_p;
cin>>s.marks_c;
sum=s.marks_e+s.marks_p+s.marks_c;
s.avg=sum/3.0f;
cout<<"Average is : "<<s.avg<<endl;
return 0;
QUESTION NO 4
#include <iostream>
struct Computer
int id;
string name;
float price;
};
int main()
Computer c1,c2;
cin>>c1.id;
cout<<"Enter First Computer name "<<endl;
cin>>c1.name;
cin>>c1.price;
cin>>c2.id;
cin>>c2.name;
cin>>c2.price;
if(c1.price>c2.price)
cout<<"Computer 1 is costly"<<endl;
cout<<"Computer id is : "<<c1.id<<endl;
else
cout<<"Computer 2 is costly"<<endl;
cout<<"Computer id is : "<<c2.id<<endl;
return 0;
}
QUESTION NO 5
#include <iostream>
struct Employee
int no,hourly_pay;
string name;
int hours_worked;
int gross_pay;
};
int main()
Employee e;
cin>>e.no;
cin>>e.name;
cin>>e.hours_worked;
cin>>e.hourly_pay;
e.gross_pay=e.hours_worked*e.hourly_pay;
return 0;
}
QUESTION NO 6
#include <iostream>
struct Complex
int real_no,imaginary_no;
};
int main()
Complex c;
cin>>c.real_no;
cin>>c.complex_no;
cout<<"Complex no = "<<c.real_no<<"+"<<c.complex_no<<"i"<<endl;
return 0;
QUESTION NO 7
#include <iostream>
struct Complex
int real_no,complex_no;
};
int main()
Complex c1,c2;
cin>>c1.complex_no;
cin>>c2.real_no;
cin>>c2.complex_no;
int t_real=c1.real_no+c2.real_no;
int t_imaginary=c1.complex_no+c2.complex_no;
cout<<"Complex no = "<<t_real<<"+"<<t_imaginary<<"i"<<endl;
return 0;
QUESTION NO 8
#include <iostream>
struct Player
int distance_covered;
int Min,sec;
};
int main()
Player p,p1,p2;
cin>>p.distance_covered;
cin>>p1.Min;
cout<<"Enter First player seconds "<<endl;
cin>>p1.sec;
cin>>p2.Min;
cin>>p2.sec;
float t_time1=p1.Min+(p1.sec/60.0f);
float t_time2=p2.Min+(p2.sec/60.0f);
if(t_time1<t_time2)
cout<<"Record is "<<endl;
else
cout<<"Record is "<<endl;
return 0;