All about C plus plus algorithms!

Tuesday 12 April 2016

Simple Calculator (With C++)

In order to make a calculator, you must have idea about if/else statements and loop in case you want the calculator to print the menu after every calculation.
Here, I have used 'do while'
do 
{
    //the thing that is to be repeated.
}
while (//condition)

The operations performed by this calculator are :
Sum
Subtract
Multiply
Divide
Magnitude
Power
ASCII of a character


Source Code:

#include <iostream>
#include  <stdlib.h>
using namespace std;
int main()
{
char redo;
do
{
cout << " *** Welcome to The Ultimate Calculator *** " << endl << endl;
cout << " If you want to ADD press 0 " << endl << " If you want to DIVIDE press 1 " << endl << " If you want to take MAGNITUDE press 2 " << endl << " If you want to take POWER press 3 " << endl << " If you want to find ASCII press 4 " << endl << " If you want to EXIT press 5 " << endl;

int x;
cin >> x;

if (x == 0)
{
cout << " You have selected ADDITION function. " << endl << endl;
cout << " Enter the Numbers you want to Add " << endl;
int a, b;
cout << " Enter the first number : ";
cin >> a;
cout << " Enter the second number : ";
cin >> b;
int sum = a + b;
cout << " The Result is " << sum << endl;

}
else if (x == 1)
{
cout << " You have selected DIVISION function. " << endl << endl;
cout << " Enter the Numerator : ";
int a,b;
cin >> a;
cout << " Enter the Denominator : ";
cin >> b;
float div = (a * 1.0) / b;
cout << " The Result is " << div << endl;
}
else if (x == 2)
{
cout << " You have selected MAGNITUDE function. " << endl << endl;
cout << " Enter the Number you want to take Magnitude of : ";
int a;
cin >> a;
if (a > 0)
cout << endl << " The Magnitude is " << a << endl;
else
{
int m = a * (-1);
cout << " The Magnitude is " << m << endl;
}
}
else if (x == 3)
{
cout << " You have selected POWER function. " << endl << endl;
cout << " Enter the Base : ";
int a;
cin >> a;
cout << " Enter the Exponent : ";
int b;
cin >> b;
int i = 1;
int prod = 1;
while (i <= b)
{
prod = a * prod;
i++;
if (i > b)
cout << " The Result is " << prod << endl;
}
}
else if (x == 4)
{
cout << " You have selected ASCII Teller. " << endl << endl;
cout << " Enter the character " << endl;
char a;
cin >> a;
cout << " It's ASCII value is " << (int)a << endl;
}
else if (x == 5)
exit(EXIT_SUCCESS);
else
{
cout << " The input you have entered is invalid! " << endl;
exit(EXIT_FAILURE);
}


cout << " Do you want to perform another Calculation? ( Y or N) : ";
cin >> redo;
cout << endl << endl;
}
while ((redo == 'y') || (redo == 'Y'));

system("pause");
return 0;
}






Hope you find it useful. :) Thank you! ^^
Share:

0 comments:

Post a Comment

Popular Posts

Blog Archive

Copyright by progrithms.blogspot.com. All Rights Reserved.. Powered by Blogger.

Copyright © Programs and Algorithms | Powered by Blogger

Design by ThemePacific | Blogger Theme by NewBloggerThemes.com