how do i go about this question in regards to for and while loops?
the user to enter two numbers and the operation to be performed , and then we display the answer . If the user type ‘A’ or ‘a’ that’s addition If the user type ‘S’ or ‘s’ that’s subtraction If the user type ‘M’ or ‘m’ that’s multiplication If the user type ‘D’ or ‘d’ that’s division
An Example of running : Enter first number : 5 Enter second number : -6 Enter the operation to be performed: M The Answer is -30
2. Modify the previous program so that it avoid dividing by zero, if the second number is a zero it will show , Division by zero is impossible
3. Modify your program so it will use repetition structure and ask the user to enter the operations and numbers repeatedly and stop when the user enter x for the operation.
The program must ask user do you want to continue Y or N . If user enter n the program must stop
cout << "please enter the first value you would like to divide by:
cin >> input1;
cout << "please enter the second value you would like to divide by";
cin >> input 2;
while (input2 == 0)
{
cout << "Sorry, you cannot divide by (0)";
cout << "please enter the first value you would like to divide by:
cin >> input1;
cout << "please enter the second value you would like to divide by";
cin >> input 2;
}
you can use switch cases, functions, and even classes and objects its all how you would like to approach the problem.
Step one and two of your question require a control statements like IF, IF...ELSE, etc and the third step requires you to use an iteration or looping statements like WHILE, DO...WHILE and FOR loops.
There are a number of ways to do it. You should look at it in its most basic form and what you are going to need.
You absolutely need 2 variables to hold the two integers ( or whatever type) you are going to apply your arithmetic operation.
int a;
int b;
You have to parse ( decipher, translate, understand) what the user wants you to do.
There are many ways to do this. If/else chains would work. So would a switch.
You have a deal breaker in division, so you absolutely need a condition check in division to make sure zero is not the divisor.
int firstnumber, secondnumber;
int Total,multiply,divide;
int M = firstnumber * secondnumber;
int D = firstnumber / secondnumber;
char maths = ' ';
char yesno;
cout<<"Please enter M for Multiplication or D for Division and press Enter. ";
cin >> maths ;
maths = toupper(maths);
cout << endl << maths << endl;
cout << "Please enter the first number: ";
cin >> firstnumber;
cout << "Please enter the second number : ";
cin >> secondnumber;
if(maths == 'M') {
cout << "the multiplication of the two numbers is:" <<M << endl;
}
else if (maths == 'D') {
cout << "the division of the two numbers is:" <<D << endl;
while (secondnumber == 0)
{
cout << "Sorry, you cannot divide by (0)";
cin >> firstnumber;
cout << "please enter the second number you would like to divide by: ";
cin >> secondnumber;
}
cout<<"Do you want to continue enter Y or N to stop: ";
cin>>yesno;
yesno = tolower(yesno);
#include <iostream>
#include <cmath>
int main()
{
usingnamespace std;
int firstnumber, secondnumber;
int Total,multiply,divide;
int M = firstnumber * secondnumber;
int D = firstnumber / secondnumber;// you CANT do this without checking for zero
// It can crash your program
char maths = ' ';
char yesno;
cout<<"Please enter M for Multiplication or D for Division and press Enter. ";
cin >> maths ;
maths = toupper(maths);
cout << endl << maths << endl;
cout << "Please enter the first number: ";
cin >> firstnumber;
cout << "Please enter the second number : ";
cin >> secondnumber;
if(maths == 'M') {
cout << "the multiplication of the two numbers is:" <<M << endl;
}
elseif (maths == 'D') {
cout << "the division of the two numbers is:" <<D << endl;
while (secondnumber == 0)
{
cout << "Sorry, you cannot divide by (0)";
cin >> firstnumber;
cout << "please enter the second number you would like to divide by: ";
cin >> secondnumber;
}
cout<<"Do you want to continue enter Y or N to stop: ";
cin>>yesno;
yesno = tolower(yesno);
system ("pause");
return 0;
}
}
On line 12 you do this division BEFORE you check for a zero divisor. Don't because if the divisor is zero it will crash your program.
#include <iostream>
// #include <cmath> // not needed
usingnamespace std;
int main()
{
int firstnumber, secondnumber;
int Total, multiply, divide;
int M;
int D;
char maths;
char yesno = 'y';
do{
cout << "Please enter M for Multiplication or D for Division and press Enter. ";
cin >> maths;
maths = toupper(maths);
//cout << endl << maths << endl; //not needed
cout << "Please enter the first number: ";
cin >> firstnumber;
cout << "Please enter the second number : ";
cin >> secondnumber;
M = firstnumber * secondnumber;
if (maths == 'M') {
cout << "the multiplication of the two numbers is:" << M << endl;
}
elseif (maths == 'D') {
if (secondnumber == 0)
cout << "Division by zero is undefined\n";
else cout << "the division of the two numbers is :"
<< firstnumber / secondnumber << "\n";
}
cout << "\nDo you want to continue enter Y or N to stop: ";
cin >> yesno;
yesno = tolower(yesno);
} while (yesno == 'y');
// system("pause"); not needed
return 0;
}