I'm still new to C. I wrote some code, but I'm sure I did something wrong because it's not giving the correct output.
(compiling and running on linux)
Code:void main() { unsigned long startingNumber; // THE Number. Is it prime? printf("Enter Positive Number (0 to 4,294,967,295) to be Tested : "); scanf(startingNumber); printf("\nTesting ", startingNumber, "...\n"); unsigned long testNumber = (startingNumber / 2); // A number can't be evenly divided by a number bigger than half the first number. unsigned long answer = startingNumber % testNumber; // Get the initial remainder. while (answer != 0 && testNumber > 1) // Decrement while checking for a modulus of 0. { answer = startingNumber % testNumber; testNumber = testNumber - 1; } if ((answer == 0) && ((startingNumber / 2) > 1) || (startingNumber == 2)) { // Check for prime, with fixes for 2, and testNumbers 1 or smaller. printf(startingNumber, " is NOT a PRIME Number\n"); } else printf(startingNumber, " is a PRIME Number\n"); return 0; };
Output:
Using GDB, the program exits with code 011.Code:fuzzypig@fuzzypig-ubuntu:~/Documents/source/primechk/c$ ./a.out Enter Positive Number (0 to 4,294,967,295) to be Tested : 265952 Testing Hfῼロ�fuzzypig@fuzzypig-ubuntu:~/Documents/source/primechk/c$
Can someone help me fix my code?



LinkBack URL
About LinkBacks





