Lab 7
Lab 7
LAB OBJECTIVE:
The switch () statement is used in place of a chain of if()…else if()…else if()…else if()…else
construct, where such a chain does not lead to very readable, follow-able code. The switch ()
statement is designed for this situation.
INTRODUCTION:
The statements which are used to execute only specific block of statements in a series of blocks
are called case control statements.
IN-LAB TASKS
Task# Write a program to check whether number is EVEN or ODD using switch
Code:
#include<stdio.h>
main()
{
int num;
printf("enter a number");
scanf("%d",&num);
switch ((num%2))
{
case 0:
printf(" \n even number \n");
break;
case 1:
printf("odd number\n");
break;
default:
printf("wrong entry");
}
console window
Code:
#include<stdio.h>
main()
{
int num1,num2,result;
char ch;
printf("Enter the operator \n a to add \n b to subtract \nc
to multiply \n d to divide:");
scanf("%c",&ch);
printf("\n enter two numbers");
scanf("%d%d",&num1,&num2);
switch(ch)
{
case 'a':
result=num1+num2;
printf("\n result we get%d",result);
break;
case 'b':
result=num1-num2;
printf("\n result we get%d",result);
break;
case 'c':
result=num1*num2;
printf("\n result we get%d",result);
break;
case 'd':
result=num1/num2;
printf("\n result we get%d",result);
break;
default:
printf("\n wrong number");
}
console window
Task# Write a program which to find the grace marks for a student using switch. The user
should enter the class obtained by the student and the number of subjects he has failed in.
If the student gets first class and the number of subjects, he failed in is greater than 3, then he
does not get any grace. If the number of subjects he failed in is less than or equal to 3 then the
grace is of 5 marks per subject.
If the student gets second class and the number of subjects, he failed in is greater than 2, then he
does not get any grace. If the number of subjects he failed in is less than or equal to 2 then the
grace is of 4 marks per subject.
If the student gets third class and the number of subjects, he failed in is greater than 1, then he
does not get any grace. If the number of subjects he failed in is equal to 1 then the grace is of 5
marks per subject.
Code:
#include<stdio.h>
main()
{
int failed,grace=0,grade;
printf("Enter grade have been taken\n");
scanf("%d",&grade);
printf("Enter failed subjects \n");
scanf("%d",&failed);
switch(grade)
{
case 1:
if(failed>3)
grace=0;
else
grace=5;
break;
case 2:
if(failed>2)
grace=0;
else
grace=4;
break;
case 3:
if(failed>1)
grace=0;
else
grace=5;
break;
default:
printf("\n wrong");
}
if(grade==1 || grade==2 || grade==3)
printf("\n grace marks %d add to per subject",grace);
console window
POST-LAB TASKS
Task# Write a C program for a menu driven program which has following options:
1. Factorial of a number.
2. Prime or not
3. Odd or even
4. Exit
Once a menu item is selected the appropriate action should be taken and once this action is
finished, the menu should reappear. Unless the user selects the 'Exit' option the program should
continue to work. Make flow chart for this task as well.
Code:
#include<stdio.h>
main ()
{
int c, num,i,fact=1,prime=1;
while(c!=4)
{
printf("Enter the choice\n 1.for factorial \n 2.for
prime numbers \n 3.for even/odd \n 4.for exit\n");
scanf("%d",&c);
switch(c)
{
case 1:
break;
case 3:
printf("\n enter the number\n");
scanf("%d",num);
if((num%2==0))
{
printf("\n %d number is even",num);
break;
}
else
{
printf("\n %d number is odd",num);
break;
}
case 4:
printf("\n exit");
break;
}
}
console window
Conclusion: Using of switch case instead of if else , to make our work easier.