Wednesday, 9 September 2020

Making a simple calculator using C


Program to make calculator using C


#include <stdio.h>
int main()
{
char choice;
float num1,num2,result;
int flag=1;
printf("Enter +,-,/,* for knowing the result ");
scanf("%c",&choice);
printf("Enter first number ");
scanf("%f",&num1);
printf("Enter second number ");
scanf("%f",&num2);
switch(choice)
{
case '+':result=num1+num2;
break;
case '-':result=num1-num2;
break;
case '/':
{
if(num2==0)
{
flag=0;

}
else
{
result=num1/num2;

}
break;

}
case '*': result=num1*num2;
break;
default:printf("Error");
break;

}
if(flag==1)
{
printf("%f %c %f = %f",num1,choice,num2,result);

}
else
{
printf("%f %c %f = undefined ",num1,choice,num2);

}

}
Output:
Output of simple addition


Output when num2=0
Description:
In this program,we ask the user to input two numbers along with the operator which they want to do.And using the switch case function, we calculate the value as user wants.



1 comment:

  1. Sir my economic situation is very poor so that I have no any calculator or digital devices to calculate numbers. I used to calculate numbers using my fingers as ancient people. But after reading this program I made a calculator myself and started calculating on it. You're really great sir. Thank you for evertything.

    ReplyDelete

If you have any doubt, please ask in the comment section.

Search This Blog

Program to print weather the number is happy or not.

#include <stdio.h> #include <math.h> int main () { int i , j , num , temp , sum = 0 ; printf ( "Enter number\n...