Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2
#include <stdio.
h> // Include the standard input/output library
int main() { // Declare integer variables to store two numbers, the result, and the user's choice int num1, num2, answer, choice;
// Prompt user for their choice of arithmetic operation
printf("Enter your choice\n"); printf("1. Addition\n"); printf("2. Subtraction\n"); printf("3. Multiplication\n"); printf("4. Division\n"); scanf("%d", &choice); // Read the user's choice
// Prompt user to enter two integers
printf("Enter num1 and num2 values: "); scanf("%d %d", &num1, &num2); // Read the two numbers
// Switch statement to perform the operation based on user choice
switch (choice) { case 1: // Addition case answer = num1 + num2; // Calculate the sum printf("Addition of %d and %d is %d\n", num1, num2, answer); // Print the result break; // Exit the switch statement case 2: // Subtraction case answer = num1 - num2; // Calculate the difference printf("Subtraction of %d from %d is %d\n", num2, num1, answer); // Print the result break; // Exit the switch statement case 3: // Multiplication case answer = num1 * num2; // Calculate the product printf("Multiplication of %d and %d is %d\n", num1, num2, answer); // Print the result break; // Exit the switch statement case 4: // Division case if (num2 != 0) { // Check for division by zero answer = num1 / num2; // Calculate the quotient printf("Division of %d by %d is %d\n", num1, num2, answer); // Print the result } else { printf("Error: Division by zero is not allowed.\n"); // Print error message } break; // Exit the switch statement default: // Handle invalid choice printf("Invalid choice\n"); // Print error message }
return 0; // Indicate successful completion of the program