Design a calculator

Program 4: Program to Create Calculator

Intro Text

For every problem we need to analyse it and need to create a solution which is efficient enough use. Here the requirement is to create a program to create a simple calculator. We need to do operations like addition, subtraction, multiplication, division and also square of that number. so here we give a option and it should process a certain block of code. as we saw in program 2 we will use switch statement in this. In general we will get a two number and we will get a choice and the we will perform operation on the two operands

This program requires a additional care. (i.e) For operations like addition, subtraction, multiplication and division requires two operands but square of a number requires single operand. so I first get the option which i want to perform then I used function to get one number or two numbers based on the operations.

Functions:

Functions helps to reduce the code as well as encourages the re-usability of the code.In C we need to declare the function and the we need to define it. We will declare the function as follows

 void getTwoNum(void);
 void getANum(void);

Where the first void is the return type of the function and the void with in the () states that the function does not take any argument.After declaring a function we need to define it.  We defined two functions for this program one function will get two operand and another will get single operand.

 //Function to get two oprand
 void getTwoNum(){
 printf("Enter the First Number...");
 scanf("%f",&num1);
 printf("Enter the Second Number...");
 scanf("%f",&num2);
 }
 //function to get a single operand
 void getANum(){
 printf("Enter a Number...");
 scanf("%f",&num1);
 }

After creating this function we will use it multiple times in our program. The final flow will be like the program prompt us to enter a choice . based on the choice the defined functions will run to get one or two numbers. then we will perform some arithmetic operations on the numbers. then we will display the output. The complete program is given below if you have any doubts kindly send a mail to admin@genbays.com .

Program:
 /*
 Experiment:Calculator to perform following operations
 addition, subtraction, multiplication, division, square
 */
 #include<stdio.h>
 #include<conio.h>
 void getTwoNum(void);
 void getANum(void);
 float num1,num2,result;
 
 void main(){
	int choice;
	clrscr();
	printf("\nCALCULATOR");
	printf("\n**********");
	printf("\n1.Additon\n2.Subtraction\n3.Multiplication\n4.Division\n5.square");
	printf("\nEnter your choice...");
	scanf("%d",&choice);
	switch(choice){
		case 1: getTwoNum();
				result=num1+num2;break;
		case 2: getTwoNum();
				result=num1-num2;break;
		case 3: getTwoNum();
				result=num1*num2;break;
		case 4: getTwoNum();
				result=num1/num2;break;
		case 5: getANum();
				result=num1*num1;break;
		default: printf("\nWorng Choice...");
				getch();
				exit(0);
	}
	printf("\nResult is...%.2f",result);
	getch();
 }
 
 void getTwoNum(){
	printf("Enter the First Number...");
	scanf("%f",&num1);
	printf("Enter the Second Number...");
	scanf("%f",&num2);
 }
 void getANum(){
	printf("Enter a Number...");
	scanf("%f",&num1);
 }