C program to Make a Simple Calculator Taking two Inputs & calculate their Addition, Subtraction. Multiplication, Division : SOLVED

Problem Statement

Write a C program to Make a Simple Calculator (Taking two Inputs & calculate their Addition, Subtraction. Multiplication, Division).

Source Code

//Write a C program to Make a Simple Calculator (Taking two Inputs & calculate their Addition, Subtraction. Multiplication, Division).

#include <stdio.h>
#include <stdlib.h>

void main(){
	float var1,var2,ans;
	int ch;
	
	//Taking inputs
	printf("Enter Variable 1:\t");
	scanf("%f",&var1);
	printf("Enter Variable 2:\t");
	scanf("%f",&var2);
	
	do{
		//Printing Menu
		printf("\n***********Calculator*************\n1.\tAddition\n2.\tSubtraction\n3.\tMultiplication\n4.\tDivision\n0.EXIT\n");
		scanf("%d",&ch);
		//Switch case for selecting operation
		switch(ch){
			case 0:
				break;
			case 1:
				ans=var1+var2;
				printf("Answer is:\t%.2f",ans);
				break;
			case 2:
				ans=var1-var2;
				printf("Answer is:\t%.2f",ans);
				break;
			case 3:
				ans=var1*var2;
				printf("Answer is:\t%.2f",ans);
				break;
			case 4:
				ans=var1/var2;
				printf("Answer is:\t%.2f",ans);
				break;
			default: 
				printf("wrong input");
		}
	}while(ch!=0);
}

Input

Enter Variable 1: 5.2
Enter Variable 2: 2.6

Output

***********Calculator*************
1.      Addition
2.      Subtraction
3.      Multiplication
4.      Division
0.EXIT
1
Answer is:      7.80
***********Calculator*************
1.      Addition
2.      Subtraction
3.      Multiplication
4.      Division
0.EXIT
2
Answer is:      2.60
***********Calculator*************
1.      Addition
2.      Subtraction
3.      Multiplication
4.      Division
0.EXIT
4
Answer is:      2.00
***********Calculator*************
1.      Addition
2.      Subtraction
3.      Multiplication
4.      Division
0.EXIT
3
Answer is:      13.52
***********Calculator*************
1.      Addition
2.      Subtraction
3.      Multiplication
4.      Division
0.EXIT
0

Leave a Comment

Your email address will not be published. Required fields are marked *