Calculator program in C


Prev Tutorial Next Tutorial

C program for calculator

In C language you can add, subtract, multiply, divide any number, these all operation you can perform by using switch case.

C Program for calculator

Calculator program in C

#include<stdio.h>
#include<conio.h>

void main()
{
char choice;
int a,b,res=0;
clrscr();
printf("Enter First value: ");
scanf("%d",&a);
printf("\n Enter Operator: ");
choice=getch();
printf("\n Enter Second value: ");
scanf("%d",&b);

switch(choice)
{
case '+':
  res=a+b;
  printf("Sum: %d",res);
break;
case '-':
  res=a-b;
  printf("Difference: %d",res);
break;
case '*':
  res=a*b;
  printf("Product: %d",res);
break;
case '/':
  res=a/b;
  printf("Quotient: %d",res);
break;
default:
  printf("Enter Valid Operator!!");
}
getch();
}

Output

Enter First number: 6
Enter operator : +
Enter Second number: 10
Sum: 16

Explanation: In above code choice=getch(); is used for scan or get one value by using getch() function from keyboard, and store in choice.


Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements