Wednesday, 11 March 2015

Performing Arithmetic Operations Using Switch...Case in C


#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2, ch ;
clrscr() ;
printf("Enter the first number : ") ;
scanf("%d", &n1) ;
printf("\nEnter the second number : ") ;
scanf("%d", &n2) ;
printf("\n[1] -> Addition ") ;
printf("\n[2] -> Subtraction ") ;
printf("\n[3] -> Multiplication ") ;
printf("\n[4] -> Division ") ;
printf("\n\nEnter your choice <1...4> : ") ;
scanf("%d", &ch) ;
switch(ch)
{
case 1 :
printf("\n%d + %d = %d", n1, n2, n1 + n2) ;
break ;
case 2 :
printf("\n%d - %d = %d", n1, n2, n1 - n2) ;
break ;
case 3 :
printf("\n%d * %d = %d", n1, n2, n1 * n2);
break ;
case 4 :
printf("\n%d / %d = %.2f", n1, n2, (float)n1 / n2);
break ;
default :
printf("\nInvalid choice");
break ;
}
getch();
}




RUN 1 :
~~~~~~~
Enter the first number : 10
Enter the second number : 5
[1] -> Addition
[2] -> Subtraction
[3] -> Multiplication
[4] -> Division
Enter your choice <1...4> : 3
10 * 5 = 50

No comments:

Post a Comment