/* A prgram to print the grade of a student accoring to the %age of marks he/she obtained. The condotions are as follows:
if %age is between 1 to 30 then Fail
30 to 40 then E
40 to 50 then D
50 to 60 then C
60 to 70 then B
70 to 80 then B+
80 to 90 then A
90 to 100 then A+ */
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,total,m2,m3,m4,m5,total=0,avg=0;
char ch;
clrscr();
m:
printf("Enter the marks in subjects : ");
scanf(" %d %d %d %d %d ",&m1,&m2,&m3,&m4,&m5);
total=m1+m2+m3+m4+m5;
printf("\nTotal is =%d",total);
avg=(m1+m2+m3+m4+m5)/5;
switch(avg/10)
{
case 1:
case 2:
{ printf(" Fail ");
break;
}
case 3:
{ printf("Grade E");
break;
}
case 4:
{
printf("GRade D");
break;
}
case 5:
{
printf("Grade C");
break;
}
case 6:
{
printf("Grade B");
break;
}
case 7:
{
printf("Grade B+");
break;
}
case 8:
{
printf("Grade A");
break;
}
case 9:
case 10:
{
printf("Grade A+");
break;
}
default:
printf("Input marks of atleast one subject is invalid ");
printf("\n\nWant to input again y/n ?");
ch=getch();
if((ch=='y') || (ch=='Y') || (ch==13))
goto m;
}
getch();
}
Note: Program seems to be very simple as it is. But what will happen if we do not write the code in switch..." avg/10"
As the program execute avg will give two digit number and if write switch(avg) as CONDITIONAL OPERATIONS ARE NOT VALID IN SWITCH. IT ALLOWS ONLY INTEGER OR CHARACTER TYPES OF DATA then we will have to write
case 1:
case 2:
case3:
.
.
.
.upto
case 29:
{
printf("Fail");
similarly for Grade E
case 30:
case 31:
.
.
.
upto case 39:
{
printf("Grade E");
program become very long.... thats why in switch statement there is applied a code "avg/10" which gives an integer between 1 to 10 as avg is declared int type of data.