mercredi 16 septembre 2020

C prime number bug

I want to find if a number is a prime number or not using the square root method. My number is working fine for different numbers when I test it but I know there is a bug that I can not find. Any help will be highly appreciated.

#include <math.h>

int is_prime(int n)
{
    int i,root;

    if(n == 2){
        return 1;
    }
    if(n % 2 == 0){
        return 0;
    }

    root = sqrt(n);

    for(i = 3; i <= root; i = i + 2){
        if(n % i == 0){
            return 0;
        }
    }

    return 1;
}

int main()
{
    int n,m;

    while (1){
        printf("Please enter a number(enter 0 to exit):");
        scanf("%d", &n);
        if(n == 0){
            break;
        }
        if(1 == is_prime(n)){
            printf("%d is a prime number.\n", n);
        }
        else{
            printf("%d is not a prime number.\n", n);
        }
    }

    return 0;
} 

Aucun commentaire:

Enregistrer un commentaire