Aim:
Write a C++ program for calculator.
Theory:
iostream: C++
input/output streams are primarily defined by iostream, a header file that is
part of the C++ standard library (the name stands for Input/Output Stream). In
C++ and its predecessor, the C programming language, there is no special syntax
for streaming data input or output.
Namespace
std: std is the standard namespace. cout, cin and a lot
of other things are defined in it. (This means that one way to call them is by
using std::cout and std::cin.) The keyword using technically means, use this
whenever you can.
Program:
#include<iostream>
using namespace std;
class calculator
{
int
a,b,sum,sub,mul,div,rem;
public:
void
getdata()
{
cout<<”Enter the numbers”;
cin>>a>>b;
}
void
addition()
{
sum=a+b;
cout<<”Addition of two numbers is:”<<sum;
}
void
substraction()
{
sub=a-b;
cout<<”Substraction of two numbers
is:”<<sub;
}
void
multiplication()
{
mul=a*b;
cout<<”Multiplication of two numbers
is:”<<mul;
}
void
division()
{
div=a/b;
cout<<”Division of two numbers is:”<<div;
}
void
remainder()
{
rem=a%b;
cout<<”Reminder of two numbers is:”<<rem;
}
};
int main()
{
int
choice;
calculator
c1;
cout<<”Enter
your choice: \n1.Addition\n 2.Substraction\n 3.Multiplication\n
4.Division\n
5. Remainder\n”;
cin>>choice;
c1.getdata();
switch(choice)
{
case 1: c1.addition();
break;
case 2: c1.substraction();
break;
case 3: c1.multiplication();
break;
case 4: c1.division();
break;
case 5: c1.remainder();
break;
default:cout<<”enter the correct choice”;
break;
}
return
0;
}
Conclusion:
Thus we have implemented program for calculator which
include all arithmetic operations.






0 comments:
Post a Comment