Lab Exercise Solution 4 &5
Lab Exercise Solution 4 &5
WEEK – 2 & 3
4. Write a C program to accept marks scored in a subject and print the result
as below.
If the marks scored is less than 35 print result as fail, if the marks scored is 35
and above and less than 50 print the result as III class, if the marks is 50 and
above and less than 60 print result as II class, if the marks is 60 and above
and less than 85 print result as I class and if the marks is 85 and above and
less than 100 print the result as distinction. [Use if-else statement]
Step 1: Get the five subject mark from the user and calculate the sum and
average and then check for the condition like
by accepting the operator from the user. Use switch case statement to do
this program.
[Hint: the given operator is +, then program should give sum of given two
integers, if the operator is – , then calculate difference, if operator is * then
calculate product and if operator is /, then calculate quotient
Solution:
#include<stdio.h>
void main()
{
int first_no,second_no,result;
char option;
printf("enter the first number");
scanf("%d",&first_no);
printf("enter the second number");
scanf("%d",&second_no);
printf("enter the option + , -, *, /\n");
option=getch();
switch(option)
{
case '+':
result=first_no+second_no;
printf("addition of two numbers = %d",result);
break;
case '-':
result=first_no-second_no;
printf("subtraction of two numbers = %d",result);
break;
case '*':
result=first_no*second_no;
printf("multiplication of two numbers = %d",result);
break;
case '/':
result=first_no/second_no;
printf("division of two numbers = %d",result);
break;
default:
printf("operator is in out of option");
break;
}
getch();
}
6. Program to use switch statement. Display Monday to Sunday
Logic to do this program
If you type ‘M’ button then it has to display as Monday , ‘T’ display as
Tuesday , ‘W’ display as Wednesday, ‘T’ as Thursday, ‘F’ as Friday , ‘R’ as
Saturday and ‘S’ as Sunday.