0% found this document useful (0 votes)
5 views1 page

ITE 12 Do While Loop

This document contains a C program that implements a simple calculator with a menu for addition, subtraction, division, and multiplication. It prompts the user to select an operation and input two numbers, then performs the selected operation while handling invalid choices and division by zero. The results are displayed with two decimal precision.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

ITE 12 Do While Loop

This document contains a C program that implements a simple calculator with a menu for addition, subtraction, division, and multiplication. It prompts the user to select an operation and input two numbers, then performs the selected operation while handling invalid choices and division by zero. The results are displayed with two decimal precision.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>

int main() {
int choice;
float num1, num2, result;

do {
// Display menu
printf("\nMath Function\n");
printf("[1] Addition\n");
printf("[2] Subtraction\n");
printf("[3] Division\n");
printf("[4] Multiplication\n");
printf("Enter your choice: ");
scanf("%d", &choice);

// Check for valid input


if (choice < 1 || choice > 4) {
printf("Invalid Choice! Enter again.\n");
}

} while (choice < 1 || choice > 4); // Repeat if choice is invalid

// Get user inputs for numbers


printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);

// Perform operation based on choice


switch (choice) {
case 1:
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case 2:
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case 3:
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f\n", result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
case 4:
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
}

return 0;
}

You might also like