Write a program that asks the user to enter a number that\'s positive and a mult
ID: 3558518 • Letter: W
Question
Write a program that asks the user to enter a number that's positive and a multiple of 4. The user has three attempts to find such a number. Write the program using a for-loop and a 'break' statement in the loop. Write the program using a while-loop and using an invert operator '! ' on the whole condition. The while-loop should look like: while(! ( Condition 1 And/Or C'ondition2 )) Write the program using a while-loop without having a global invert operator '! '. You may still use the invert operator for every individual condition. The while-loop should look like: while( Condition 1 And/Or Condition2) Enter a number that's positive and a multiple of 4 You have 3 attempts Input: 5 Wrong! Try again! Input: 12 Correct! It took you 2 attempts!Explanation / Answer
// PROGRAM 1
#include<iostream>
using namespace std;
int main()
{
int number;
int attempts = 3;
cout <<"Enter a number that's positive and a multiple of 4 you have 3 attempts :" << endl;
cout <<"Input :";
cin >> number;
cout << endl;
// using for loop: and break statement.
for(attempts = 3 ; attempts>0; attempts--)
{
if(number%4 == 0)
{
cout <<"Correct! It took you "<<(4-attempts)<< " attempts !" << endl;
break;
}
else
{
cout << "Wrong! Try again !" << endl;
cout <<"Input :";
cin >> number;
cout << endl;
}
}
if(!attempts)
cout <<"Sorry u didnt enter number in 3 attempts." << endl;
//system("pause");
return 0;
}
// PROGRAM 2
#include<iostream>
using namespace std;
int main()
{
int number;
int attempts = 3;
cout <<"Enter a number that's positive and a multiple of 4 you have 3 attempts :" << endl;
cout <<"Input :";
cin >> number;
cout << endl;
// using while and invert operator ....
while(!((number%4==0) || !(--attempts)))
{
cout << "Wrong! Try again !" << endl;
cout <<"Input :";
cin >> number;
cout << endl;
}
if(!attempts)
{
cout <<"Sorry u didnt enter number in 3 attempts." << endl;
}
else
cout <<"Correct! It took you "<<(4-attempts)<< " attempts !" << endl;
//system("pause");
return 0;
}
// PROGRAM 3
#include<iostream>
using namespace std;
int main()
{
int number;
int attempts = 4;
cout <<"Enter a number that's positive and a multiple of 4 you have 3 attempts :" << endl;
cout <<"Input :";
cin >> number;
cout << endl;
// using while and && operator ....
while(--attempts && number%4!=0)
{
cout << "Wrong! Try again !" << endl;
cout <<"Input :";
cin >> number;
cout << endl;
}
if(!attempts)
{
cout <<"Sorry u didnt enter number in 3 attempts." << endl;
}
else
cout <<"Correct! It took you "<<(4-attempts)<< " attempts !" << endl;
//system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.