prime number code

Write a program that prints all prime numbers up to an input number n. Your progam must allow me to put in any number, then using a for loop you must output all prime numbers up to that number. (You must use a for loop in your code). Each prime number must be on it's on line.
This is not a homework site. We won't do your homework for you. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again. As it is impossible to find derivative of function without knowledge in arithmetic, you cannot do more complex tasks in programming without clear understanding of basics
closed account (48bpfSEw)
Look how easy the solution is:

https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Understanding the problem ist the first step to solve it.

How would you explain this methode of Eratosthenes to a child?
Write it down. Coding is the next step.
i was told to look up a program for this and when i couldnt find one i asked the forum
i understand it however i am new to c++ so im having trouble writing it
hows this:
#include <iostream>

using namespace std;
int main()
{
int N;
cin >> N;
for (int i = 2; N > 0; ++i)
{
bool isPrime = true ;
for (int j = 2; j < i; ++j)
{
if (i % j == 0)
{
isPrime = false ;
break ;
}
}
if (isPrime)
{
--N;
cout << i << "\n";
}
}
return 0;
}
Please always use code tags: http://www.cplusplus.com/articles/z13hAqkS/

Doing things like that will help you get more replies :+)
closed account (48bpfSEw)
There is something wrong with the code. For N = 10 it puts this out:

2

3

5

7

11

13

17

19

23

29

The correct set woud be: 3,5,7


try again! you are on the right path! ^^
Last edited on
Topic archived. No new replies allowed.