C Program switch case: "Operator as case in switch cases" with output

 #include <stdio.h>

int main() { double num1, num2, result; char operator; printf("Enter the first number: "); scanf("%lf", &num1); printf("Enter the operator (+, -, *, /): "); scanf(" %c", &operator); // Note the space before %c to consume any leading whitespace. printf("Enter the second number: "); scanf("%lf", &num2); switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 != 0) { result = num1 / num2; } else { printf("Error: Division by zero!\n"); return 1; // Exit the program with an error code } break; default: printf("Invalid operator\n"); return 1; // Exit the program with an error code } printf("Result: %lf\n", result); return 0; }





Enter the first number: 10 Enter the operator (+, -, *, /): + Enter the second number: 5 Result: 15.000000

Comments

Popular posts from this blog

Video Formats

Where video editing is used

File saving and folder management