Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

c program for addition, subtraction, multiplication and division using switch

 C Program for addition, subtraction, multiplication and division using switch


  1. #include

  2. int main() {
  3.     int num1, num2, choice;
  4.     float result;
  5.     printf("Enter two numbers: ");
  6.     scanf("%d %d", &num1, &num2);
  7.     printf("Enter your choice: \n1 for addition\n2 for subtraction\n3 for multiplication\n4 for division\n");
  8.     scanf("%d", &choice);

  9.     switch(choice) {
  10.         case 1:
  11.             result = num1 + num2;
  12.             printf("Addition: %.2f\n", result);
  13.             break;
  14.         case 2:
  15.             result = num1 - num2;
  16.             printf("Subtraction: %.2f\n", result);
  17.             break;
  18.         case 3:
  19.             result = num1 * num2;
  20.             printf("Multiplication: %.2f\n", result);
  21.             break;
  22.         case 4:
  23.             result = (float)num1 / num2;
  24.             printf("Division: %.2f\n", result);
  25.             break;
  26.         default:
  27.             printf("Invalid choice!\n");
  28.     }
  29.     return 0;
  30. }


  • the user is prompted to enter two numbers and then a choice for the operation to be performed. The choice is taken as an integer input and is used in the switch statement. 
  • Depending on the user's choice, the program performs the corresponding operation using the two numbers as input.
  • It's worth noting that, in the division operation, if the second number is zero, it will cause a runtime error because division by zero is not defined. 
  • I have used type casting to convert the int variables to float so as to get the decimal value. Also, I have used the format specifier %.2f to print the result with 2 decimal places.
  • the user is prompted to enter two numbers and then a choice for the operation to be performed. The choice is taken as an integer input using the scanf function and is used in the switch statement. Depending on the user's choice, the program performs the corresponding operation using the two numbers as input.

  • The switch statement checks for the value of the variable choice. Depending on the value, the program enters the corresponding case and performs the operation. The break statement is used at the end of each case to exit the switch statement and prevent the program from executing the next case.
  • In the case of the division operation, I have used type casting to convert the int variables to float so as to get the decimal value. Also, I have used the format specifier %.2f to print the result with 2 decimal places.

  • In the default case, if the user enters any value other than 1, 2, 3, or 4, the program displays an "Invalid choice!" message. This is to handle the scenario when the user enters an unexpected value.


This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

c program for addition, subtraction, multiplication and division using switch

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×