user3911561
user3911561

Reputation: 43

Find if given number is prime or not

I am trying to find if 5915587277 is prime or not. This number is actually prime and I am expecting this from my program. When I run this program it says that it is not prime and its divisor is 199.

#include<stdio.h>

int main()
{
    long n = 0;
    long i = 0;
    printf("Enter Number: ");
    scanf("%ld", &n);
    long m = n/2;

    if(n%2 == 0)
    {
        printf("Not Prime");
        return 0;
    }

    for( i = 3; i <= m; i++)
    {
        if(n%i == 0)
        {
            printf("Not Prime: %d\n", i);
            return 0;
        }

    }

    printf("Prime");
    return 0;
}

I am not sure why this code is printing this number as NOT prime when it is.

Upvotes: 2

Views: 456

Answers (2)

Avinash
Avinash

Reputation: 497

Which type of compiler you are using? if it supports 64 bit numbers then you can do that. Another thing is that use the unsigned long integer to store the maximum value. your code is just fine but you need to brush up your knowledge in max value can be stored by particular data type. Hope this answer may help you.

Upvotes: 0

Tyler
Tyler

Reputation: 750

You are exceeding the max value for a long. See Data type limits.

Upvotes: 9

Related Questions