7.6 Character Arithmetic Operation
7.6 Character Arithmetic Operation
[22] Perform basic arithmetic operations. At first, take two integers and then a symbol of the arithmetic operation as input.
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Reminder (%)
Power (^)
সমাধান
[22] Perform basic arithmetic operations. At first, take two integers and then a symbol of the arithmetic operation as input.
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Reminder (%)
Power (^)
#include<stdio.h>
#include<math.h>
int main(){
    int num1, num2;
    char op;
    
    scanf("%d%d %c", &num1, &num2, &op);
    switch(op){
        case '+':
            printf("Result: %d\n", num1 + num2);
            break;
        case '-':
            printf("Result: %d\n", num1 - num2);
            break;
        case '*':
            printf("Result: %d\n", num1 * num2);
            break;
        case '/':
            if (num2 != 0) {
                printf("Result: %f\n", (float)num1 / num2);
            } else {
                printf("Division by zero is not allowed.\n");
            }
            break;
        case '%':
            if (num2 != 0) {
                printf("Result: %d\n", num1 % num2);
            } else {
                printf("Division by zero is not allowed.\n");
            }
            break;
        case '^':
            printf("Result: %d\n", (int)pow(num1, num2));
            break;
        default:
            printf("Invalid operation.\n");
            break;
    }
    return 0;
}Last updated