Powered By Blogger

Friday, April 20, 2012

Program : 5



PROGRAM - 5(a)

Write a program, which takes two integers operands and one operator from the user, performs the operation and then prints the result. (Consider the operations +,-,*,/,%, use switch statement).

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
char oper;
int num1,num2,result;
clrscr();
printf("enter num1 oper num2 : ");
scanf("%d %c %d",&num1,&oper,&num2);
switch (oper)
{
case '+' :
           result=num1+num2;
           break;
case '-' :
           result=num1 - num2;
           break;
case '*' :
           result=num1 * num2;
           break;
case '/' :
           result=num1 / num2;
           break;
case '%' :
           result=num1 % num2;
           break;
default :
           printf("unknown operator");
           break;
}
printf("%d %c %d = %d",num1,oper,num2,result);
getch();
}

/*   OUTPUT: enter num1 oper num2 :2 + 3
                2+3=5
*/


PROGRAM - 5(b)

Write a program 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. Use the following rules:
ü  If the student gets first class and the number of subjects failed in is > 3, then no grace marks are awarded. If the number of subjects he has failed in is <= 3 then the grace is 5 marks per subject.
ü  If the student gets second class and the number of subjects failed in is >2, then no grace marks are awarded. If the number of subjects failed in is <= 2 then the grace is 4 marks per subject.
ü  If the student gets third class and the number of subjects failed in is >1, then no grace marks are awarded. If the number of subjects failed in is =1 then the grace is 5 marks per subject. 

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
     int n,cl;
     clrscr();
     printf("Enter no.of subjects student failed 
                         and class obtained : ");
     scanf("%d %d",&n,&cl);
     if (n>3 && cl==1)
           n=1;
     else if (n<=3 && cl==1)
           n=2;
     else if (n>2 && cl==2)
           n=3;
     else if (n<=2 && cl==2)
           n=4;
     else if (n>1 && cl==3)
           n=5;
     else if (n==1 && cl==3)
           n=6;
     else {}
     switch (n)
     {
     case 1 :
           printf("\n No grace marks");
           break;
     case 2 :
           printf("\n grace is 5 marks");
           break;
     case 3 :
           printf("\n No grace marks");
           break;
     case 4 :
           printf("\n grace is 4 marks");
           break;
     case 5 :
           printf("\n No grace marks");
           break;
     case 6 :
           printf("\n grace is 5 marks");
           break;
     }
     getch();
}

No comments:

Post a Comment