In: Electrical Engineering
a) Write C code using if statements to display ‘Freshman, ‘Sophomore’, ‘Junior’ or ‘Senior’ based on what value is in variable classification. Display ‘unknown ’ if classification does not contain ‘f’, ‘s’, ‘j’or ‘r’. Allow upper or lower case in variable color. In other words either ‘r’ or ‘R’ will display ‘Senior’.
b) Write C code to do the same thing as the previous problem, but use a switch statement.
Answer :- a) The C code has been written below using if statement-
#include<stdio.h>
int main()
{
char color, flag = 0;
printf("\n Enter a character ");
scanf("%c", &color);
if(color == 'f' || color == 'F'){
printf("\n Freshman \n");
flag = 1;
}
if(color == 's' || color == 'S'){
printf("\n Sophomore \n");
flag = 1;
}
if(color == 'j' || color == 'J'){
printf("\n Junior \n");
flag = 1;
}
if(color == 'r' || color == 'R'){
printf("\n Senior \n");
flag = 1;
}
if(flag == 0)
printf("\n Unlnown \n");
return 0;
}
Answer :- b) The C code using switch statement is written below-
//Answer :- a) The C code has been written below using if statement-
#include<stdio.h>
int main()
{
char color;
printf("\n Enter a character ");
scanf("%c", &color);
switch(color){
case 'F' :
case 'f' :{
printf("\n Freshman \n");
break;
}
case 'S' :
case 's' :{
printf("\n Sophomore \n");
break;
}
case 'J' :
case 'j' :{
printf("\n Junior \n");
break;
}
case 'R' :
case 'r' :{
printf("\n Senior \n");
break;
}
default:
printf("\n Unknown \n");
}
return 0;
}