In: Computer Science
Please Use C language to Make a calculator. Make sure calculator is able to take up to 5 values. Try to make calculator using simple coding As you can.
Please create a simple calculator with only +, -,* and divide. It has to be able to enter any numbers (including decimals) and be able to do the following functions:
+, -, divide and multiply.
Please have the answers, always rounded to two decimal figures. The calculator will also have to be able to take up to 5 values.
#include <stdio.h>
int main()
{
double a,b,add,sub,mul,di;
int ch;
char ch2;
do{
printf("\nEnter the first number: ");
scanf("%lf",&a); //Taking input from user
printf("\nEnter the second number: ");
scanf("%lf",&b); //Taking input from user
printf("\nEnter 1 for addition ");
printf("\nEnter 2 for subtraction ");
printf("\nEnter 3 for multiplication ");
printf("\nEnter 4 for division ");
printf("\nEnter your choice: ");
scanf("%d",&ch); //Taking choice from user
switch(ch)
{
case 1:add=a+b; //performing addition
printf("\nAddition is:%.2lf",add); //printing the result
break;
case 2:sub=a-b; //performing subtraction
printf("\nSubtraction is:%.2lf",sub); //printing the result
break;
case 3:mul=a*b; //performing multiplication
printf("\nMultiplication is:%.2lf",mul); //printing the
result
break;
case 4:di=a/b; //performing division
printf("\nDivision is:%.2lf",di); //printing the result
break;
default:
printf("\nEnter valid choice");
}
printf("\nDo you want to continue?");
scanf("%s",&ch2); //Taking choice from user
}while(ch2=='y'||ch2=='Y');
return 0;
}
Output:-