Hello, this is my first post so I apologize if I mess up at all. I have an assignment that needs me to enter a positive integer and find every prime number that comes before entered integer. For example, if the number 25 was entered the prime numbers that would need to be expressed are 2, 3, 5, 7...23. There is no set "upper limit" but the highest value I really need to go up to is around 100. My code was going to look something like this:
int upper;
cout << "enter the upper limit: ";
cin >> upper;
if (upper < 0){
cout << "The number you entered is negative. Goodbye." << endl;
else ***
This is where I need help. I have an if statement that figures out if the number entered is negative, and now I was going to have a long else statement for the rest of the situations. I have learned about looping and switch case statements in class and am kinda getting an idea of how to do it, but it'll be long.
My idea is this:
if upper = 2
then prime number is 2
if upper = 3
then p. n is 2, 3
if 3 < upper <= 5
then p.n is 2, 3, 5
and so on and so forth until I get to around 100.
This code I have planned will be extremely long and redundant and doesn't seem like the most efficient way of doing this assignment.
I am looking for help to condense the above "code" into a more loop like code but don't know where to start. If this question doesn't make sense please let me know in the comments and I'll try rephrasing it. Again I apologize if this post is messed up in any way.
Thank you!
Hi,
I have a suggestion. Remember coding is only here to make things easier. Ask yourself if it is easier to check for prime or not(!) Prime. If you are familiar with the fundamentals such as % and ! and || i think you will find easier to create an array that prints what is prime < 100 if you can successfully translate the code of the definition of prime (× == factors of 1 and itself).
P.S. I am 100% sure there is a hundred ways of doing this and you are not alone. Search the forms you will find many answers.
Hope this helped.