Simple Calculator in C
#include <stdio.h>
void main()
{
int num1,num2; /*Declaration of variables*/
float result=0; /*Declaration of variables*/
char ch; //to store operator choice
printf("Enter first number: ");
scanf("%d",&num1); /*read first number*/
printf("Enter second number: ");
scanf("%d",&num2); //read second number
printf("Choose operation to perform (+,-,*,/,%): ");
scanf(" %c",&ch);
switch(ch)
{
case '+':result=num1+num2;
break;
case '-':result=num1-num2;
break;
case '*': result=num1*num2;
break;
case '/':result=(float)num1/(float)num2;
break;
case '%': result=num1%num2;
break;
default: printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result); //display output on screen
}
OUTPUT
LOGIC
Step 1: For using calculator we are taking two numbers from user.
Step 2: Using switch case statement we perform the addition, subtraction, multiplication and division operation.
0 Comments