Hello,

I'm very new to programming and I am working on a program to test to see if a random number is prime. This is what I have so far. I'm really stuggling with the logic of it. I understand what I have up to the while loop. I'm pretty sure my while loop is incorrect but I'm not sure how to go about fixing it. Any help would be greatly appreciated. Thanks.

the instructions for the program are:

1.) assign variable isPrime the value true
2.) get input from user and assign to variable oneNumber
3.) assign value of 2 to variable count
4.) while count is less than or equal to (they left it blank here. I assumed it is comparing to oneNumber) and isPrime is true do
if oneNumber can be divided by count then
assign isPrime the value false
else
increase count by 1
5.) output the test result

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
/*Declaring variables*/
int isPrime = 1;
int oneNumber;
int count = 2;

/*Getting input from the user*/
printf("Enter a number greater than 2: \n");
scanf("%d",&oneNumber);

while (count <= oneNumber && isPrime == 1)

    if (oneNumber % count == 0)
    {
    isPrime = 0;
    }
    else
    {
    count = count + 1;
    }
    return 0;
}

Recommended Answers

All 5 Replies

In Step 4 you only need to check up to the square root of the number (think about it for a bit and you'll see why). It will speed up your code as you'll be checking a lot less numbers.

Also, checking all the even numbers slows you down, as if it isn't divisible by 2, it's not divisible by any other even number. This means you can check 2, and if not check all the odd numbers (start your count at 3 and add 2 each time). I'm not sure if you are required to follow their steps, just throwing that out there.

Can you give me an example of what would work for step 4? I don't really understand.

Can you give me an example of what would work for step 4? I don't really understand.

Well, first check if the number is divisible by 2.

as if it isn't divisible by 2, it's not divisible by any other even number.

So,

#include <math.h>//sqrt is defined here.
if(onenumber%2!=0){
    for(count=3;count<=sqrt(onenumber);count+2){//the count need not be greater than square root of onenumber.
        if(onenumber%count==0){
            //is not prime 
        else
           //continue to test
}
else
    //not prime (is even)

Furthermore, check this wikipedia page to improve your knowledge on primality test.

I went with this:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    /*Declaring variables*/
    int oneNumber;

    /*Getting input from the user*/
    printf("Enter a number greater than 2: \n");
    scanf("%d",&oneNumber);


if (oneNumber == 3)
    {
    printf("a prime");
    return 0;
    }
    else if (oneNumber % 2 == 0)
    {
    printf("Not a prime number");
    }
    else if (oneNumber % 3 == 0)
    {
    printf("not a prime number");
    }
    else printf(" a prime number ");

return 0;
}

Oh Good Grief!

Change your program, to generate all the prime numbers up to the number the user puts in.

Then you'll get on the right track with this. This is out in the weeds.

A better challenge would be to find all the lowest 10,000 primes, and use a timer in your program to see how changes affect the running time.

You'll learn a LOT, and get out of the weeds. Of course, we'll help, OK? ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.