Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, division and modulo.
| Operator | Operation |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulo |
Example C program using arithmetic operators:
#include <stdio.h>
int main() {
int ch, input1, input2, res;
do {
printf("Menu:\n");
printf("1. Addition\n2. Subtraction\n");
printf("3. Multiplication\n3. Division\n");
printf("4. Modulo\n");
printf("Enter your choice:");
scanf("%d", &ch);
printf("Enter your input1 and input2\n");
scanf("%d%d", &input1, &input2);
switch (ch) {
case 1:
res = input1 + input2;
break;
case 2:
res = input1 - input2;
break;
case 3:
res = input1 * input2;
break;
case 4:
res = input1 / input2;
break;
case 5:
res = input1 % input2;
break;
default:
printf("You have entered wrong option\n");
break;
}
printf("Result: %d\n", res);
printf("Do you want to continue(0/1):");
scanf("%d", &ch);
} while (ch == 1);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulo
Enter your choice:1
Enter your input1 and input2
10324 8987
Result: 19311
Do you want to continue(0/1):1
Menu:
1. Addition
2. Subtraction
3. Multiplication
3. Division
4. Modulo
Enter your choice:4
Enter your input1 and input2
101 100
Result: 1
Do you want to continue(0/1):0
Menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulo
Enter your choice:1
Enter your input1 and input2
10324 8987
Result: 19311
Do you want to continue(0/1):1
Menu:
1. Addition
2. Subtraction
3. Multiplication
3. Division
4. Modulo
Enter your choice:4
Enter your input1 and input2
101 100
Result: 1
Do you want to continue(0/1):0
No comments:
Post a Comment