We have created a basic menu driven calculator using the C programming language. This code seems lengthy, but it's easy to understand when looked upon. In this program, we have used a switch statement, goto statement & if else if loop to code it. The features of this program are :
- Alerts the user about Zero division
- Allows the user to run the program again and again without closing it.
- Alerts the user about an invalid selection of choice.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main() | |
{ | |
int n,a,b; | |
start: | |
printf("Enter the value of a: "); | |
scanf("%d",&a); | |
printf("Enter the value of b: "); | |
scanf("%d",&b); | |
printf("MENU\n1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division\n5 - Modulo division"); | |
printf("\nEnter your choice: "); | |
scanf("%d",&n); | |
switch(n) | |
{ | |
case 1: | |
printf("%d + %d = %d",a,b,a+b); | |
break; | |
case 2: | |
printf("%d - %d = %d",a,b,a-b); | |
break; | |
case 3: | |
printf("%d X %d = %d",a,b,a*b); | |
break; | |
case 4: | |
if(b == 0) | |
printf("Division is not possible!"); | |
else | |
{ | |
printf("%d / %d = %f",a,b,(float)a/b); | |
} | |
break; | |
case 5: | |
printf("%d mod %d = %d",a,b,a%b); | |
break; | |
default: | |
printf("Invalid Entry!! Check the menu for choice"); | |
break; | |
} | |
end: | |
printf("\nTo terminate press 0 and to go back to the program press 1: "); | |
scanf("%d",&n); | |
if(n == 1) | |
{ | |
goto start; | |
} | |
else if(n == 0) | |
{ | |
goto terminate; | |
} | |
else | |
{ | |
printf("\nInvalid entry"); | |
goto end; | |
} | |
terminate: | |
return(0); | |
} |
Comments
Post a Comment