c += a; is equal to writing c = c + a; c -= a; is equal to writing c = c – a; c *= a; is equal to writing c = c * a; c /= a; is equal to writing c = c / a; c %= a; is equal to writing c = c % a;
In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input, using switch case statement(decision control statement).
We had written same calculator program using else-if clause. Same program has been modified to use Switch case in this program. Simple Calculator Application In C
We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.
If the user enters wrong choice, we ask the user to enter proper choice.
#include < stdio.h >
int main()
{
int a, b;
char choice;
printf("Enter your choice\n");
printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n");
scanf("%c", &choice);
printf("Enter 2 integer numbers\n");
scanf("%d %d", &a, &b);
switch(choice)
{
case 'a': printf("%d + %d = %d\n", a, b, (a+b));
break;
case 'b': printf("%d - %d = %d\n", a, b, (a-b));
break;
case 'c': printf("%d x %d = %d\n", a, b, (a*b));
break;
case 'd': if( b != 0)
printf("%d / %d = %d\n", a, b, (a/b));
else
printf("Number can't be divided by 0\n");
break;
default: printf("You entered wrong choice\n");
break;
}
return 0;
}
Output Enter your choice a. Addition b. Subtraction c. Multiplication d. Division c Enter 2 integer numbers 5 10 5 x 10 = 50
In above program we are asking user to enter character a or b or c or d to perform addition, subtraction, multiplication and division operations respectively.
printf("Enter 2 integer numbers. Format: a + b\n");
scanf("%d %c %d", &a, &choice, &b);
switch(choice)
{
case'+': printf("%d + %d = %d\n", a, b, (a+b));
break;
case'-': printf("%d - %d = %d\n", a, b, (a-b));
break;
case'*': printf("%d x %d = %d\n", a, b, (a*b));
break;
case'/': if( b != 0)
printf("%d / %d = %d\n", a, b, (a/b));
else
printf("Number can't be divided by 0\n");
break;
default: printf("You entered wrong choice\n");
break;
}
return 0;
}
</stdio.h>
#include
int main()
{
int a, b;
char choice;
printf("Enter your choice\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
scanf("%c", &choice);
printf("Enter 2 integer numbers. Format: a + b\n");
scanf("%d %c %d", &a, &choice, &b);
switch(choice)
{
case '+': printf("%d + %d = %d\n", a, b, (a+b));
break;
case '-': printf("%d - %d = %d\n", a, b, (a-b));
break;
case '*': printf("%d x %d = %d\n", a, b, (a*b));
break;
case '/': if( b != 0)
printf("%d / %d = %d\n", a, b, (a/b));
else
printf("Number can't be divided by 0\n");
break;
default: printf("You entered wrong choice\n");
break;
}
return 0;
}
Output: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division 1 Enter 2 integer numbers. Format: a + b 50 + 60 50 + 60 = 110
Here we ask the user to enter values as well as the operation to be performed. Operands and operator.
Note 1: There is no need of curly braces inside case. Note 2: If a case doesn’t end with break statement, then the execution continues and the block of code present inside next case will also get executed. Note 3: default case is optional. And the code inside it executes only when non of the cases match. Note 4: Here switch, case, break, default are all keywords / reserve words.
We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.
If the user enters wrong choice, we ask the user to enter proper choice.
printf("Addition of %d and %d is %d\n", a, b, (a+b));
}
elseif(choice == 2)
{
printf("Subtraction of %d and %d is %d\n", a, b, (a-b));
}
elseif(choice == 3)
{
printf("Multiplication of %d and %d is %d\n", a, b, (a*b));
}
elseif(choice == 4)
{
if(b != 0)
printf("Division of %d and %d is %d\n", a, b, (a/b));
else
printf("Number can not be divided by 0\n");
}
else
{
printf("Please enter any of these no 1, 2, 3, 4\n");
}
return 0;
}
#include < stdio.h >
int main()
{
int a, b, choice;
printf("Enter your choice\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n\n");
scanf("%d", &choice);
if(choice > 4)
{
printf("You entered wrong choice\n");
}
else
{
printf("Enter 2 integer numbers\n");
scanf("%d %d", &a, &b);
}
if(choice == 1)
{
printf("Addition of %d and %d is %d\n", a, b, (a+b));
}
else if(choice == 2)
{
printf("Subtraction of %d and %d is %d\n", a, b, (a-b));
}
else if(choice == 3)
{
printf("Multiplication of %d and %d is %d\n", a, b, (a*b));
}
else if(choice == 4)
{
if(b != 0)
printf("Division of %d and %d is %d\n", a, b, (a/b));
else
printf("Number can not be divided by 0\n");
}
else
{
printf("Please enter any of these no 1, 2, 3, 4\n");
}
return 0;
}
Output 1: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
1 Enter 2 integer numbers 3 2 Addition of 3 and 2 is 5
Output 2: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
2 Enter 2 integer numbers 8 3 Subtraction of 8 and 3 is 5
Output 3: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
3 Enter 2 integer numbers 5 5 Multiplication of 5 and 5 is 25
Output 4: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
4 Enter 2 integer numbers 10 2 Division of 10 and 2 is 5
Output 5: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
4 Enter 2 integer numbers 0 12 Division of 0 and 12 is 0
Output 6: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
4 Enter 2 integer numbers 10 0 Number can not be divided by 0
Output 7: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
50 You entered wrong choice Please enter any of these no 1, 2, 3, 4
Note: In division section(that is, choice 4) you can see nesting of if else statement. You can know more about nested if-else statements in this program: Nested if else Statement In C
Note: We can write the same program within while loop and ask the user if he wants to continue working inside calculator application. This avoids the need to exit and re-run the program. We’ll publish the code and video tutorial for the same in coming days. Stay subscribed to our YouTube Channel and blog.
printf("Multiplication of %d and %d is %d\n", a, b, c);
return 0;
}
#include < stdio.h >
int main()
{
int a, b, c;
printf("Enter 2 numbers for multiplication\n");
scanf("%d %d", &a, &b);
c = a * b;
printf("Multiplication of %d and %d is %d\n", a, b, c);
return 0;
}
Output: Enter 2 numbers for multiplication 25 5 Multiplication of 25 and 5 is 125
You can write same program without using third variable to calculate multiplication of 2 numbers, as below:
printf("Multiplication of %d and %d is %d\n", a, b, (a*b));
return 0;
}
#include < stdio.h >
int main()
{
int a, b;
printf("Enter 2 numbers for multiplication\n");
scanf("%d %d", &a, &b);
printf("Multiplication of %d and %d is %d\n", a, b, (a*b));
return 0;
}
Output: Enter 2 numbers for multiplication 25 5 Multiplication of 25 and 5 is 125
Note: Instead of int you can take float variables too. That would help in taking both integer as well as real values from the user as input.
Scanf(): For user input
In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program
In this video tutorial we shall learn how to swap two integer numbers without using a temporary variable and by simply making use of multiplication and division.
printf("After swapping a = %d and b = %d\n", a ,b);
return 0;
}
#include < stdio.h >
int main()
{
int a, b;
printf("Enter 2 integer numbers\n");
scanf("%d %d", &a, &b);
printf("You entered a = %d and b = %d\n", a, b);
a = a * b;
b = a / b;
a = a / b;
printf("After swapping a = %d and b = %d\n", a ,b);
return 0;
}
Output: Enter 2 integer numbers 3 5 You entered a = 3 and b = 5 After swapping a = 5 and b = 3
Scanf(): For user input
In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program
Swap 2 numbers using only Multiplication and Division: C
Swapping 2 Numbers In C using only Multiplication and Division: Logic
In our above program we are asking user to enter integer value for a and b. If the user enters 10 and 5 for a and b respectively. Then our program executes below logic to swap the values of variable a and b.
Step 1: Multiply values of a and b and store it in variable a. i.e., a = a * b; 50= 10 * 5; So a = 50;
Step 2: Now divide value of b from value of a and store it in variable b. i.e., b = a / b; 10= 50 / 5; (value of a = 50 according to Step 1) So value of variable b is now 10.
Step 3: Now divide value of b from value of a and store it in variable a. i.e., a = a / b; 5= 50 / 10; (Value of a = 50 from Step 1, and Value of b = 10 from Step 2) Now the value of a = 5;
Finally value of a = 5(from step 3) and value of b = 10(from step 2).
This is how we swap the value of variable a and b by just making use of multiplication and division in C programming language.