TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Add Subtract Multiply Divide Modulo m+ m- mc Calculator Program in C++
Ashish Srivastava
Apr 27
2016
Code
3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
double
memory=0;
double
add (
double
x)
{
double
y;
cout<<
"\n\n number:"
;
cin>>y;
cout<<
"\n"
;
return
(x+y);
}
double
multiply(
double
x)
{
double
y;
cout<<
"\n\n number:"
;
cin>>y;
cout<<
"\n"
;
cout<<x<<
"*"
<<y<<
"="
;
return
(x*y);
}
double
substract(
double
x)
{
double
y;
cout<<
"\n\n number:"
;
cin>>y;
cout<<
"\n"
;
return
(x-y);
}
double
divide(
double
x)
{
double
y;
cout <<
"\n\nnumber:"
;
cin>>y;
cout <<
"\n"
;
return
(x/y);
}
double
modulo(
double
x)
{
double
y;
cout<<
"\n\n number:"
;
cin>>y;
cout <<
"\n"
;
if
((
int
)x-x!=0 !! (
int
)y-y!=0)
throw1;
return
((
int
)x%(
int
)y);
}
int
main()
{
double
firstnumber;
double
secondnumber;
int
operand;
void
calculator(
double
firstnumber);
system(
"CLS"
);
cout<<
"A simple calculator"
<<endl;
cout <<
"number:"
;
cin>>firstnumber;
cout<<
"\n"
;
cout<<
"\n(1)add (2)subtract (3)multiply (4)divide (5)modulo\n"
<<
"\n(6)m+ (7)m- (8)mc (9)clear (10)exit\n\n"
<<endl;
cin >> operand;
try
{
switch
(operand)
{
case
1:
firstnumber=add(firstnumber);
break
;
case
2:
firstnumber=substract(firstnumber);
break
;
case
3:
firstnumber=multiply(firstnumber);
break
;
case
4:
firstnumber=divide(firstnumber);
break
;
case
5:
firstnumber=modulo(firstnumber);
break
;
case
6:
memory+=firstnumber;
firstnumber=memory;
break
;
case
7:
memory-=firstnumber;
firstnumber=memory;
break
;
case
8:
firstnumber=memory;
memory=0;
break
;
case
9:
firstnumber=0;
main();
return
0;
break
;
case
10:
return
0;
}
}
catch
(
char
*msg)
{
cout<<msg;
}
cout<<
"\n\n the answer is:"
<<firstnumber<<
"\n\n"
;
system(
"PAUSE"
);
calculator(firstnumber);
return
0;
}
void
calculator(
double
firstnumber)
{
int
operand;
system(
"CLS"
);
cout<<memory<<endl;
cout<<
"a simple calculator"
<< endl;
cout<<
"number:"
<<firstnumber<<
"\n\n"
;
cout<<
"\n(1)add (2)subtract (3)multiply (4)divide (5)modulo\n"
<<
"\n (6)m+ (7)m- (8)mc (9)clear (10)exit\n\n"
<<endl;
cin>>operand;
try
{
switch
(operand)
{
case
1:
firstnumber=add(firstnumber);
break
;
case
2:
firstnumber=substract(firstnumber);
break
;
case
3:
firstnumber=multiply(firstnumber);
break
;
case
4:
firstnumber=divide(firstnumber);
break
;
case
5:
firstnumber=modulo(firstnumber);
break
;
case
6:
memory+=firstnumber;
firstnumber=memory;
break
;
case
7:
memory-=firstnumber;
firstnumber=memory;
break
;
case
8:
firstnumber=memory;
memory=0;
break
;
case
9:
firstnumber=0;
main();
return
;
break
;
case
10:
return
;
}
}
catch
(
int
x)
{
cout<<
"% not defined for modulo\n"
;
}
cout<<
"\n\nthe answer is:"
<<firstnumber<<
"\n\n"
;
system(
"PAUSE"
);
calculator(firstnumber);
}
Add Subtract Multiply Divide Modulo m+ m- mc Calculator Program in C++