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;
}