I have to write a program that allows users to enter a number that is greater than 3 and less than or equal to 100. I then have to have the program choose the prime numbers and output them. I have to use a do- while loop with a nested while loop in the program. I am having issues with the program and I can not tel lwhat I did wrong. I am new to programming, so I do not know what I am doing wrong. The code I have is below.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int number = 2, userNum;
bool prime;
cout << "Please enter a positive integer between 3 and 100: ";
cin >> userNum; // user inputted number
cout << "____________________________________________________________\n";
do {
if ((userNum <= 3 || userNum > 100)) {
cout << "Must be a positive integer between 3 and 100: ";
cin >> userNum;
cout << "______________________________________________________\n";
}
else {
number++;
prime = true;
int divisor = 2;
while (divisor <= number - 1){
if(number % divisor == 0)
prime = false;
divisor++;
if(prime)
cout << number << "/";
}
}
}
while (number < userNum);
cout << endl << "__________________________________________________________\n";
cout << "These are all the prime numbers between 3 and " << number << endl;
You can't have a DO block without the while condition. Put your code in [code] tags please. Also, what's the problem? We can't exactly help you if you just tell us "I'm having a problem with this code. HALP!" We need more information.
I need to write a program that allows a user to enter a number in between 3 and 100. The program will then list all prime numbers from 3 to whatever number was entered. The problem is that if I enter any other number other than 3, the program just runs really fast and closes on me. I think I have an error in the first do while loop, where the user inputs the number, but I am not positive. Like I said this is all new to me, so I am just looking for any advice on how to Below should be right code. I do not know how to put it in code tags or I would. if you know how please just let me know and I will do it for you.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int number = 2, userNum;
bool prime;
cout << "Please enter a positive integer between 3 and 100: "; // User input
cin >> userNum; // user inputted number
cout << "____________________________________________________________\n";
do {
if (userNum <= 3 || userNum >= 100) {
cout << "Must be a positive integer between 3 and 100: "; //if user input is not within parameters
cin >> userNum;
cout << "______________________________________________________\n";
}
else {
number++;
prime = true;
int divisor = 2;
while (divisor <= number - 1){
if(number % divisor == 0) // calculating prime numbers
prime = false;
divisor++;
if(prime)
cout << number << ""; //show prime numbers
}
}
}
while (number < userNum);
cout << endl << "__________________________________________________________\n";
cout << "These are all the prime numbers between 3 and " << number << endl;
I figured it out... My head was clearly stuck firmly somewhere when I did the first code. The right code is below. Thank you for attempting to help thoguh.
include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int userNum;
bool enteredValid = true;
do // do-while for user input
{
if (!enteredValid)
cout << " Must be a positive integer between 3 and 100. \n";
enteredValid = false;
cout << "Enter a positive integer between 3 and 100: ";
cin >> userNum;
}
while ((userNum < 3) || (userNum > 100)); // do-while for user input