Menu Driven Program to perform Add/ Subtract/ Multiply/ Divide/ Modulus Using C

Just starting to learn C and you want to make your own calculator? Fear not! Here, we show you how it is done using the language C, explaining everything we do and why we do it.

First lets C the code!

#include<stdio.h> int main() { int choice, a, b ; float result;  printf("Enter Choice"); printf(" 1 - Mulitply(*)  2 - Divide(/)  3 - Remainder(\%\%)  4 - Add(+)  5 - Subtract (-)  6 - Exit");//to print \% operator we write \%\% two times, so compiler doesnt mistake it for an identifier scanf("\%d", &choice);  printf("Enter two numbers to be operated upon: "); scanf("\%d \%d", &a, &b);  switch(choice)//user's choice is checked { //We store the operation value in the variable 'result' case 1://if user entered 1 result = a*b; break;  case 2://if user entered 2 result = (float)a/b; //type casting break;  case 3: //if user entered 3 result = a\%b; break;  case 4: result = a+b; break;  case 5: result = a-b; break;  case 6: printf(" Exit "); break;  default: //in case the user inputs Wrong choiceprintf("Invalid Choice"); return 0;//Exit the Program itself, by returning 0 to main }  printf("The result is \%f",result);//result is float so we use \%f  return 0; //return 0 indicates that there isnt any kind of error in the program and it has been executed successfully }

Output

Menu Driven C program for Calculator

Explanation:

Variable ‘ a, b ‘ holds two integer variables

Variable ‘ choice’ is used to check which of the following operations are chosen by the user

Variable ‘ result ‘ is float to handle fractional operations

switch(choice)  {  case 1: Some Statements; Break; case 2 : Some Second Statement; Break;  default: Executed if no case is matching.  }

The parameter passed on to switch statement is ‘ choice ‘ based on which the case inside the switch may or may not get executed. Here we have choice as an ‘integer’ but it may also be ‘characters’ like case ‘a’ or case ‘+’.

Try the program using characters ‘ + ’ , ‘ — ’, ‘ * ’, etc. in switch and case!! It’s easy

Note: Condition is written inside single inverted commas if we want to check case with character.

The value of ‘choice’ is checked with value of case condition: if matched the statements of that particular case is executed.

If none of the cases are a match then the default case statements are executed. Here we display message as “Invalid Choice” for default case, and exit the program itself because what’s the use of taking variables if computer doesn’t know what to do with them? Simply return 0; which returns to function main() skipping all the statements ahead!

All done operating? Lets print the result now! Look below

printf("The result is \%f",result);

We use identifier “ \%f “ because we know ‘ result ‘ is declared as float variable. Duh! Try printing it with “ \%d “ you will get a garbage value (Duh Again..!)

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don’t forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.


Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Image Sharpening by High Pass Filter using Python and OpenCV
17 August 2018

Explaining Register variables in C with examples
17 August 2018

C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий