You did not fix your scanf() problem. As I said, scanf requires an the address of a variable. Line 34 of last post.
This is a discussion on Another beginning function within the C Programming forums, part of the General Programming Boards category; You did not fix your scanf() problem. As I said, scanf requires an the address of a variable. Line 34 ...
You did not fix your scanf() problem. As I said, scanf requires an the address of a variable. Line 34 of last post.
It works now, but its not giving me primes. Can't believe I had & in the wrong place...Code://Jeremiah A. Walker//ThousandthPrime: Calculate the thousandthprime. #include <stdio.h> #include <stdbool.h> //primality function which will determine if a number is prime. long Primality(long n) { //check to see if the number is a multiple of 2(ie the number is even) and is not equal to 2 if(n % 2 == 0 && n != 2) { return false; //if the number is even or equal to 2, return false and do not count these numbers }//end if //once we determine that a number is not even or equal to 2, we can check the odds. for(long i = 3; i * i <= n; i += 2) { if(n % i == 0) { return true; }//end if }//end for return false; }//end function primality int main() { long primes = 0;// counts the number of primes for us. long n = 1; //variable that we will run through primality function long y = 0; //variable input by the user to tell how many primes to find. printf("Please enter a number(n), and the program will return with the \"nth\" number of primes: "); scanf("%ld", &y); //while loop calls the primality functions until "y" primes are found while(primes < y) { n++; Primality(n); if(Primality(n)) { printf("%ld\n", n); primes++; }//end if }//end while printf("I found %ld primes %ld is the \"nth\" prime.\n", primes, n); }//end main
Indeed. The OP did not suggest the diagnostics listed were the only ones emitted by the compiler though.
Given that the code in the first post only has the string "false" within a comment, and does not contain the string "true" at all, it is a fair bet that information provided is incomplete.
Works now. Thanks for all your help.Code://Jeremiah A. Walker //ThousandthPrime: Calculate the thousandthprime. #include <stdio.h> #include <stdbool.h> //primality function which will determine if a number is prime. long Primality(long n) { //check to see if the number is a multiple of 2(ie the number is even) and is not equal to 2 if(n % 2 == 0 && n != 2) { return false; //if the number is even or equal to 2, return false and do not count these numbers }//end if //once we determine that a number is not even or equal to 2, we can check the odds. for(long i = 3; i * i <= n; i += 2) { if(n % i == 0) { return false; }//end if }//end for return true; }//end function primality int main() { long primes = 0;// counts the number of primes for us. long n = 1; //variable that we will run through primality function long y = 0; //variable input by the user to tell how many primes to find. printf("Please enter a number(n), and the program will return with the \"nth\" number of primes: "); scanf("%ld", &y); //while loop calls the primality functions until "y" primes are found while(primes < y) { n++; Primality(n); if(Primality(n)) { printf("%ld\n", n); primes++; }//end if }//end while printf("I found %ld primes %ld is the \"nth\" prime.\n", primes, n); }//end main
Please enter a number(n), and the program will return with the "nth" number of primes: 100 2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229
233
239
241
251
257
263
269
271
277
281
283
293
307
311
313
317
331
337
347
349
353
359
367
373
379
383
389
397
401
409
419
421
431
433
439
443
449
457
461
463
467
479
487
491
499
503
509
521
523
541
I found 100 primes 541 is the "nth" prime.