Program to make a Simple Calculator in C.

 

This program takes two operands from user and perform the Calculations on the two operands depending upon the operator entered by the user.

 

 

Let’s write the following code to make simple calculator in C.

 

Step 1: Let’s open turboC and write the foolowing code.

 

// Code

 

//Add preprocessor

 

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

int d;

float a,b,c;

 

\ Clear Screen

clrscr();

 

printf(“enter first number:”);

scanf(“%f”,&a);

printf(“enter second number:”);

scanf(“%f”,&b);

printf(“ \n Select any option: \n 1.Add \n 2.Sub \n 3.Mul\n 4. Div \n 5. Exit \n”);

scanf(“%d”,&d);

 

// Using Switch

switch(d)

{

case 1: c=a+b;

printf(“result = %f“,c);

break;

case 2: c=a-b;

printf(“result=%f”,c);

break;

case 3:c=a*b;

printf(“result=%f”,c);

break;

case 4:c=a/b;

printf(“result=%f”,c);

break;

default:

printf(“Invalid choice”);

break;

}

getch();

}

 

Step 2: Run the program and see output:

 

Output:

 

output

 

Happy Coding.

 

 

 

 

 

 

 

 

 

 

 

 

 

Tags: , ,