Here is a program that performs arithmetic operations using switch case in C programming language. The use of switch case provides more control to the user by asking the user which operation he/she wants to perform.
#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 of above program
Enter the first number : 15
Enter the second number : 3
[1] -> Addition
[2] -> Subtraction
[3] -> Multiplication
[4] -> Division
Enter your choice <1...4> : 4
15 / 3 = 5.00
Explanation of above program
The program first asks user to enter two numbers, then it shows four options to the user. When user enters a choice the program take that value and passes it to the switch-case statement. Here the program picks a case based on the value that user entered before and executes that particular case. If no case matches the users request, default case will be executed.
If you notice you'll see that inside each case there is a break statement. Now what is the need to use break statement inside each case? Read it here - Why use break statement inside switch case.
Hello There,
I am shocked, shocked, that there is such article exist!! But I really think you did a great job highlighting some of the key Performing Arithmetic Operations all about technology in the entire space.
I want to use fgets to read a line of text and a word. The text is examined to find the word, but the word is never found even when the word is in the text.
What am I doing wrong?
Great effort, I wish I saw it earlier. Would have saved my day :)
Merci Beaucoup,
Daniel