Write a simple calculator program in C C++

 

Flowchart

Simple calculator flowchart

 

Coding in C

#include<stdio.h>
int main()
{
char choice;
int n1,n2,res;
printf(“Enter choice: “);
scanf(“%c”,&choice);
printf(“Enter two numbers: “);
scanf(“%d%d”,&n1,&n2);
switch(choice)
{
case ‘+’:
res=n1+n2;
break;
case ‘-‘:
res=n1-n2;
break;
case ‘*’:
res=n1*n2;
break;
case ‘/’:
res=n1/n2;
break;
case ‘%’:
res=n1%n2;
break;
}
printf(“Result = %d”,res);
return 0;
}

 

Coding in C++

#include<iostream>
using namespace std;
int main()
{
char choice;
int n1,n2,res;
cout<<“Enter choice: “;
cin>>choice;
cout<<“Enter two numbers: “;
cin>>n1>>n2;
switch(choice)
{
case ‘+’:
res=n1+n2;
break;
case ‘-‘:
res=n1-n2;
break;
case ‘*’:
res=n1*n2;
break;
case ‘/’:
res=n1/n2;
break;
case ‘%’:
res=n1%n2;
break;
}
cout<<“Result = “<<res;
return 0;
}

 

Output

Simple calculator output

 

Published by

admin

I am a computer teacher, programmer and web developer