Hi im suppose to write a for loop inside a while loop where i ask the user to input a number between 0 and 501. and then when the number input is in the range im suppose to write a for loop that shows the even numbers between 0 and the number picked. This is what i have so far.
#include <iostream>
using namespace std;
int main()
{
// Display header (Name , program title, and program objective) cout << "Miguel Lopez" << endl << "Quiz 2 - conditionals and loops""\n";
cout << "Successfully making a program with conditionals and loops""\n";
//Ask the user for a number between 0 and 501
1 2 3
int number;
int count = 1;
while (count < 500)
{
cout << "Enter a number between 0 and 501: ";
cin >> number;
if (number < 1 || number > 500)
{
cout << "\nOUT OF RANGE!";
}
}
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
#include <iostream>
usingnamespace std;
int main()
{
// Display header (Name , program title, and program objective)
cout << "Miguel Lopez" << endl << "Quiz 2 - conditionals and loops""\n";
cout << "Successfully making a program with conditionals and loops""\n";
//Ask the user for a number between 0 and 501
int number;
int count = 1;
while (count < 500)
{
cout << "Enter a number between 0 and 501: ";
cin >> number;
if (number < 1 || number > 500)
{
cout << "\n OUT OF RANGE!\n";
}
// <--- What do you do if the number is in range?
// <--- And where does the value of "count" change??
}
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
// Display header (Name , program title, and program objective)
cout
<< "Miguel Lopez\n"
<< "Quiz 2 - conditionals and loops\n"
<< "Successfully making a program with conditionals and loops\n";
//Ask the user for a number between 0 and 501
int number{0};
bool keep_on_entering{true};
while (
keep_on_entering == true &&
cout << "Enter a number between 0 and 501: " &&
cin >> number
)
{
if( (number < 1 || number > 500) )
{
keep_on_entering = true;
cout << "OUT OF RANGE!\n";
}
else
{
keep_on_entering = false;
for(int i = 0; i < number; i++)
{
// blah blah
}
cout << "finished\n";
}
}
return 0;
}
Miguel Lopez
Quiz 2 - conditionals and loops
Successfully making a program with conditionals and loops
Enter a number between 0 and 501: 0
OUT OF RANGE!
Enter a number between 0 and 501: -2
OUT OF RANGE!
Enter a number between 0 and 501: 501
OUT OF RANGE!
Enter a number between 0 and 501: 500
finished
Program ended with exit code: 0
I am not sure what you expect for output but I did come up with this:
Miguel Lopez
Quiz 2 - conditionals and loops
Successfully making a program with conditionals and loops
Enter a number between 0 and 501: 0
OUT OF RANGE!
Enter a number between 0 and 501: 600
OUT OF RANGE!
Enter a number between 0 and 501: 20
2 4 6 8 10 12
14 16 18
Enter a number between 0 and 501: 43
2 4 6 8 10 12
14 16 18 20 22 24
26 28 30 32 34 36
38 40 42
Enter a number between 0 and 501: 65
2 4 6 8 10 12
14 16 18 20 22 24
26 28 30 32 34 36
38 40 42 44 46 48
50 52 54 56 58 60
62 64
#include <iostream>
#include <limits>
usingnamespace std;
int main()
{
cout
<< "Miguel Lopez\n"
<< "Quiz 2 - conditionals and loops\n"
<< "Successfully making a program with conditionals and loops\n";
int number {};
while ((cout << "Enter a number between 0 and 501: ") && (!(cin >> number) || number < 1 || number > 500)) {
if (!cin) {
cout << "Not a number\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else
cout << "Not in range\n";
}
for (int i = 0; i <= number; i += 2)
cout << i << ' ';
cout << '\n';
}