# Thread: printing only prime numbers

1. ## printing only prime numbers

I have one more problem:
I need to displey number of prime numbers. Number is enetered from user.
I need to use only while loops.
I tried, but having problems-infinite loop

Code:
```#include <stdio.h>

int main()
{
int n,i=1,prime=1;
printf("How many prime numbers do you want: ");
scanf("%d",&n);
while(n)
{
i=1;
while(i<=prime)
{
if(prime%i==0)
break;
i++;
}
if(prime==i)
{
printf(" %d",prime);
n--;

}
prime++;

}
return 0;
}```
Thanks

2. You need to start i at 2 not 1. All integers numbers are divisible by 1 so the case of (prime %i ==0) will always be true.

3. Thanks!
I really cannot believe I didn't see this.
Code:
```#include <stdio.h>

int main()
{
int n,i=1,prime=1;
printf("How many prime numbers do you want: ");
scanf("%d",&n);
while(n)
{

while(i<=prime)
{
if(prime%i==0)
{

break;
}
i++;
}
if(prime==i)
{
printf(" %d",prime);
n--;

}
prime++;
i=2;

}
return 0;
}```

4. You should initalize i to 2 during declaration also.

5. Well, in that case it will not be displayed 1

6. 1 is not a prime number. The first prime is 2.

7. First look up the definition of Prime number . It says a prime number is one which is divisible by one and the number itself.It also must not be divisible by any other number. So 1 wont be a prime. 2 will be a prime .

8. Well by the definiton you provided, 1 is divisible by 1 and itself and isn't divisible by any other number, so it would be a prime. It's classed as a special case (i.e. "Well there's no real reason why and it's difficult to express so we'll just do it to be awkward"), isn't it?

9. A prime number (or prime integer, often simply called a "prime" for short) is a positive integer p > 1 that has no positive integer divisors other than 1 and p itself.
1 is excluded from the definition. Here is my backup.
Math World

More concisely, a prime number p is a positive integer having exactly one positive divisor other than 1. (emphasis added)

11. I can't believe all these replies about such minor issue as is 1 prime number or not.
Anyway my homework was to print 1,2,..etc. So I was supposed to write program that will print 1 and others. Thanks for help.
I would like to see similar discussion on my dijkstra algorithm problem but...

12. >I can't believe all this threads about such minor issue as is 1 prime number or not.
And you've been hanging around here long enough to have over 100 posts?

>So I was supposed to write program that will print 1 and others.
Code:
```printf("%d ", 1);
print_primes();```

13. ## Let me have a go at it

Originally Posted by Micko
I can't believe all these replies about such minor issue as is 1 prime number or not.
Anyway my homework was to print 1,2,..etc. So I was supposed to write program that will print 1 and others. Thanks for help.
I would like to see similar discussion on my dijkstra algorithm problem but...
Unfortunetly, in some cases, this community is very helpful and we thrive on helping each other in what ways we can. even when ppl are just trying to get us to do their homework. some of us will attempt the solution to a problem simply to satisfy our intellectual curiousity.
Code:
```#include <stdio.h>
int main()
{
int n,i=1,prime=1;
printf("How many prime numbers do you want: ");
scanf("%d",&n);

/*always check scanf's return value. this case check to make sure n > 0 and is
an integer. type a letter when first run this program to see why */

while(n)

/*as long as n is not zero the code will run so unless user enters
'0' code will run accordingly better to put while ( (n > 1) ),theoretically;
but you already cause an infinite with the next statement */

{
i=1;
/*already declared i as 1 why declared again?
this will make sure i is ALWAYS 1 */

while(i<=prime)
/* prime is declared as 1 so saying while 1 is <= 1 */
{
if(prime%i==0){/* if the remainder of 1 / 1 == 0  break; this is true as there
is no remainder it evaluates to 0*/

printf("\nprime is now %d, i is now %d: ", prime, i);
printf("Press [enter] to continue your code: ");
getchar();
/*this is a test statement. it'll show you the program state. take it out for
poop and giggles */

break;
/* a break statement in a loop causes the program to break of that loop that
encloses it and go to the next stage of the program. In this case if prime == i print
prime */
}
i++;
/* this doesn't matter anyway cause you put i back to 1 again */
}
if(prime==i)
{
printf("%d\n",prime);
/* this will only print once and n will only decrease once
another infinite loop :d you need to read up on loops and rememebr when in doubt
paretheses */
n--;

}
prime++;/*prime will always increase but i won't because: you have this statement : */
i=2;
/* why even have this assignement cause you declare it back to 1 after so i
will forever be 1*/

}
printf("\nfinished doing what u told me.\nnotice anything strange?\n");
system("pause");
return 0;
}```

14. >some of us will attempt the solution to a problem simply to satisfy our intellectual curiousity.
But those of us that could help the most by posting a complete solution to homework satisfy our "intellectual curiosity" and don't post it. Doing someone else's homework does more harm than good.

><snip code>
Tabs and spaces don't mix well. It's usually better if you only use spaces and format your comments and mix them with code to be easier to read. As it is I find your program uncomfortable.

15. Originally Posted by Prelude
>some of us will attempt the solution to a problem simply to satisfy our intellectual curiousity.
But those of us that could help the most by posting a complete solution to homework satisfy our "intellectual curiosity" and don't post it. Doing someone else's homework does more harm than good.
I don't think I solved it. I was merely showing the individual a way to think in order to come to a satisfactory conclusion. or the expected results.

><snip code>
Tabs and spaces don't mix well. It's usually better if you only use spaces and format your comments and mix them with code to be easier to read. As it is I find your program uncomfortable.
don't be afraid of the code, it won't touch you inapropiately or anything. actaully i was trying to find the vb code tags to format it better, but i couldn't.

In anycase the issue is that this individual needs help on how to go about getting the desired solution. If s/he pastes the code and run it - which they will have to do - the code will not only be easier to read but s/he can also check the program state, which is more important.