Answer to Question #27890 in C++ for faiz

Question #27890
Question 2: Write a Simple Calculator Using If Else Statement that prompts the user to Input two Numbers. Then it will Display the Menu for Addition, Subtraction, Multiplication, Division, Square of 1st Number, Cube of 1st Number, Sin, Cos, Tan. After any choice it will Display the Answer
1
Expert's answer
2013-04-09T08:55:46-0400
#include <iostream>
#include <math.h>

int main()
{
float x,y;

//Show messages to enter numbers
std::cout <<"Enter two numbers.\n";
std::cout <<"Enter first number x:\n";
std::cin >> x;
std::cout <<"Enter second number y:\n";
std::cin >> y;

int n;
do
{
//Show menu:
std::cout << "Choose option:\n";
std::cout << "1- Add (x+y)\n";
std::cout << "2- Subtract (x-y)\n";
std::cout << "3- Multiply (x*y)\n";
std::cout << "4- Divide (x/y)\n";
std::cout << "5- Square of x (x^2)\n";
std::cout << "6- Cube of x (x^3)\n";
std::cout << "7- Sin of x (sin(x))\n";
std::cout << "8- Cos of x (cos(x))\n";
std::cout << "9- Tan of x (tan(x))\n";
std::cout << "0 - exit\n";
std::cin >>n;
}
while ( ( n < 0 ) || ( n > 9 ) );

float result;
//calculate result according to input:
if ( n == 1 )
{
result = x+y;
}

if ( n == 2 )
{
result = x-y;
}

if ( n == 3 )
{
result = x*y;
}

if ( n == 4 )
{
if( y != 0 )
{
result = x/y;
}
else
{
std::cout << "Wrong division parameters ( y=0 )!\n";
return -1;
}
}

if ( n == 5 )
{
result = x*x;
}

if ( n == 6 )
{
result = x*x*x;
}

if ( n == 7 )
{
result = sin( x );
}

if ( n == 8 )
{
result = cos( x );
}

if ( n == 9 )
{
result = tan( x );
}

if ( n != 0 )
{
std::cout <<"Result=" <<result <<"\n";
}
return 0;
}


// other variant of calculator, using switch statement
#include <iostream>
#include <math.h>

int main()
{
float x,y;

//Show messages to enter numbers
std::cout <<"Enter two numbers.\n";
std::cout <<"Enter first number x:\n";
std::cin >> x;
std::cout <<"Enter second number y:\n";
std::cin >> y;

int n;
do
{
& //Show menu:
& std::cout << "Choose option:\n";
& std::cout << "1- Add (x+y)\n";
& std::cout << "2- Subtract (x-y)\n";
& std::cout << "3- Multiply (x*y)\n";
& std::cout << "4- Divide (x/y)\n";
& std::cout << "5- Square of x (x^2)\n";
& std::cout << "6- Cube of x (x^3)\n";
& std::cout << "7- Sin of x (sin(x))\n";
& std::cout << "8- Cos of x (cos(x))\n";
& std::cout << "9- Tan of x (tan(x))\n";
& std::cout << "0 - exit\n";
& std::cin >>n;
}
while ( ( n < 0 ) || ( n > 9 ) );

float result;
//calculate result according to input:

switch ( n )
{
case 1:
& result = x+y;
& break;
case 2:
& result = x-y;
& break;
case 3:
& result = x*y;
& break;
case 4:
& if( y != 0 )
& {
& result = x/y;
& }
& else
& {
& std::cout << "Wrong division parameters ( y=0 )!\n";&
& return -1;
& }
& break;
case 5:
& result = x*x;
& break;
case 6:
& result = x*x*x;
& break;
case 7:
& result = sin( x );
& break;
case 8:
& result = cos( x );
case 9:
& result = tan( x );
& break;
}

if ( n != 0 )
{
& std::cout <<"Result=" <<result <<"\n";
}
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS