Create a Union called student with the following details as variables within it.
1. Name of the student
2. Age
3. Year of study
4. Semester
5. Five different subject marks in array
Write a C++ program to create an object for the union to access these and print the Name, age,
year, semester and grade according to their percentage of marks scored.
90 % and above – S grade
80% to 89% -- A grade
70% to 79% -- B grade
60% to 69% -- C grade
50% to 59% -- D grade
<50% -- F grade
Implementation of the above problem:
#include <iostream.h>
union Student
{
char name[25],grade;
int age,year,semester;
float m[5];
}s1;
int main()
{
int i,j;
int total=0,average;
char grade;
cout<<"\nEnter the student's name : ";
cin>>s1.name;
cout<<"\nName : "<<s1.name;
cout<<"\n----------------------\n";
cout<<"\nEnter the age";
cin>>s1.age;
cout<<"\nAge : "<<s1.age;
cout<<"\n----------------------\n";
cout<<"\nEnter the year";
cin>>s1.year;
cout<<"\nYear : "<<s1.year;
cout<<"\n----------------------\n";
cout<<"Enter semester ";
cin>>s1.semester;
cout<<"\nSemester : "<<s1.semester;
cout<<"\n----------------------\n";
cout<<"Enter the five different marks for the student :";
for(i=0;i<5;i++)
{
cin>>s1.m[i];
if(s1.m[i]>=50)
total=total+s1.m[i];
else
j++;
}
average=total/5;
if(average>=90)
{ grade='S';}
else if(average>=80 && average<90)
{ grade='A';}
else if(average>=70 && average<80)
{ grade='B';}
else if(average>=60 && average<70)
{ grade='C';}
else if(average>=50 && average<60)
{ grade='D';}
else if(j>=1)
{grade='F';}
cout<<"\nGrade : "<<grade<<"\n";
cout<<"Size of the union is :"<<sizeof(s1);
return 0;
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: