I am currently learning C++ (beginner level) and my assignment was to build a program that will display all the prime numbers between 1 and 100.

I wrote this algorithm:#include <iostream>

using namespace std;int main() {

int x, y, z;

for(x=1; x <= 100; x++) {

for(y=x-1; 0 < y; y--) {

z = x % y;

if(z!=0) cout << x << "\n";

}

}

return 0; }Which just displays all the numbers between 1 and 100 over and overâ€¦

My compiler is Visual Studio 2010.

What can I do to make it work?

Thanks!

hi, i tried to write the code but it was harder than i though (at least for me) because i am a beginner too. but i found the code on the net and i understood it so i hope you will like it. #include <iostream>

using namespace std;int NUMBER_ENTERD;

bool IS_PRIME = true;

int main()

{

while(true)

{

//intro

cout << "Enter a number: ";

cin >> NUMBER_ENTERD;

cout << "\n........................................ \n";

//prime checker

//first check if the number is greater than 1

if(NUMBER_ENTERD > 1)

{

for(int t=2; t<NUMBER_ENTERD; t++)

{

if(NUMBER_ENTERD % t == 0)

{

IS_PRIME = false;

}

}

}

else

{

IS_PRIME = false;

}

//print the result

if(IS_PRIME == true)

{

cout << "The number " << NUMBER_ENTERD << " is prime\n";

}

else

{

cout << "The number " << NUMBER_ENTERD << " is not prime\n";

}cout << "........................................ \n\n\n\n";

system("PAUSE");

cout << "\n\n\n";

}

return 0;

}

ooooh you know there is something wrong with this code, i will try to fix it help me if you can

hey i am sorry about that, but i found the wrong phrase is in the (If else loop )

else

{

IS_PRIME = false;

}

it should beelse

{

IS_PRIME = true;

}and there just the loop from 1 to 100 i am sure you can write it

The next thing you need to think about: Is 1 a prime number?

hi Razor2.3 I think you write but can you tell me why either its in math or in programing?? thank you

Ask Your Question

Weekly Poll

Do you think Microsoft's new Surface Go will be a hit?

Discuss in The Lounge

Poll History