Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find if given number is prime or not

Tags:

c

primes

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.

like image 980
user3911561 Avatar asked Dec 21 '25 02:12

user3911561


1 Answers

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

like image 84
Tyler Avatar answered Dec 22 '25 17:12

Tyler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!