Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

STUCTURE AND UNION EXAMPLE C PROGRAM

#include<stdio.h>
#include<conio.h>
// Structure Definition
struct stud
{       
            int rno;
            char name[10];
            float m1,m2,m3;
};
struct stud s;//Structure Variable Declaration
//Union Definition
union result
{
            char grade;
            float percentage;
};
Union result r; //Union variable Declaration
void main()
{
            int choice;
            float per,temp,total;
            clrscr();
            printf("\nEnter your Roll No");
            scanf("%d",&s.rno);
            printf("\nEnter your Name");
            scanf("%s",&s.name);
            printf("\nEnter your mark1");
            scanf("%f",&s.m1);
            printf("\nEnter your mark2");
            scanf("%f",&s.m2);
            printf("\nEnter your mark3");
            scanf("%f",&s.m3);
            total=s.m1+s.m2+s.m3;
            temp=(float)(total/300);
            r.percentage=temp*100;
            per=r.percentage;
            printf("\nEnter your choice");
            printf("\n1.Percentage\n2.Grade");
            scanf("%d",&choice);
            printf("\nyour name is %s",s.name);
            printf("\nyour roll no is %d",s.rno);
            printf("\nyour total is%f",total);
switch(choice)
{
            case 1:             printf("\nyour percentage is %f",r.percentage);
                                    break;
            case 2:            if(per<=100 && per>=90)
                                    {r.grade='S';}
                                    else if(per<=90 && per>=80)
                                    {r.grade='A';}
                                    else if(per<=80 && per>=70)
                                    {r.grade='B';}
                                    else if(per<=70 && per>=60)
                                    {r.grade='C';}
                                    else if(per<=60 && per>=55)
                                    {r.grade='D';}
                                    else if(per<=55 && per>=50)
                                    {r.grade='E';}
                                    else
                                    {r.grade='U';}
                                    printf("\nYour grade is%c",r.grade);
                                    break;
}
getch();
}


No comments:

Post a Comment

Flag Counter