Showing posts with label Projects in C. Show all posts
Showing posts with label Projects in C. Show all posts

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.



Search This Blog

Labels

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...