Performing arithmetic operations using switch...case
Program:
#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();
}
Output:
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
-
UpdatedDec 30, 2019
-
Views16,234
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order