C Tutorial
This c program is used to perform the simple calculator functions and continues till user quits the calculator.
Below calculator program displays menu using operators +, *, -, /, and % or q, then user has to enter the operation to perform.
#include<stdio.h> void main() { int val1, val2; char op; while(1) { printf("\nCalculator"); printf("\n--------------"); printf("\n + Add\n * Multiplication\n - Subtraction\n / Division\n % Modulus\n q Quit"); printf("\nEnter Operation: "); scanf(" %c", &op); switch(op) { case '+': printf("\nEnter Value 1: "); scanf("%d", &val1); printf("\nEnter Value 2: "); scanf("%d", &val2); printf("\nAddition of %d and %d: %d", val1, val2, val1+val2); break; case '*': printf("\nEnter Value 1: "); scanf("%d", &val1); printf("\nEnter Value 2: "); scanf("%d", &val2); printf("\nMultiplication of %d and %d: %d", val1, val2, val1*val2); break; case '-': printf("\nEnter Value 1: "); scanf("%d", &val1); printf("\nEnter Value 2: "); scanf("%d", &val2); printf("\nSubtraction of %d and %d: %d", val1, val2, val1-val2); break; case '/': printf("\nEnter Value 1: "); scanf("%d", &val1); printf("\nEnter Value 2: "); scanf("%d", &val2); printf("\nDivision of %d and %d: %d", val1, val2, val1/val2); break; case '%': printf("\nEnter Value 1: "); scanf("%d", &val1); printf("\nEnter Value 2: "); scanf("%d", &val2); printf("\nModulo of %d and %d: %d", val1, val2, val1%val2); break; case 'q': return; default: printf("\nInvalid operator!"); } } }
Output:
$ cc calculator.c $ ./a.out Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus Enter Operation: + Enter Value 1: 4 Enter Value 2: 5 Addition of 4 and 5: 9 Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus Enter Operation: / Enter Value 1: 25 Enter Value 2: 5 Division of 25 and 5: 5 $ ./a.out Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus q Quit Enter Operation: q
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page