C program to find a prime number

☆樱花仙子☆ 提交于 2019-12-08 07:16:52

问题


I wrote a C program which tells whether a given number is prime or not. But it has a problem in it. It is working fine for numbers other than multiples of 5. But it is showing the multiples of 5 as prime like 15, 25, 35, 45... . I am not able to find the error. I've tried comparing it with other programs on the internet but I am not able to find the error.

#include <stdio.h>

int primeornot(int a) {
    int i;

    for (i = 2; i <= a / 2; i++) {
        if (a % i == 0) {
            return 0;
            break;
        } else {
            return 1;
        }
    }
}

main() {
    int number_given_by_user;

    printf("Enter a positive integer to find whether it is prime or not : ");
    scanf("%d", &number_given_by_user);

    if (primeornot(number_given_by_user)) {
        printf("The given number is a prime number");
    } else {
        printf("The given number is not a prime number");
    }
}

回答1:


Not only multiples of 5 (for example, 9 is also considered prime by your code)

Your code is flawed. You are using a loop but only checking the first iteration, since you have a return inside each branch of the condition inside your loop:

for(i=2;i<=a/2;i++)
{
    if(a % i == 0)
    {  
        return 0;    // <------- (this one is fine, since finding a divisor means outright that this number isn't a prime)
        break;       //  also, a break after a return is redundant
    }
    else
    {
        return 1;    // <------- (however, this one is flawed)
    }
}

In this form, your code only does return !(input % 2) which is not a very good prime finding algorithm :-)

What you need to do is check all the iteration, and only if all of them go to the else branch, the number is prime.

So, change to:

int primeornot(int a)
{
int i;

for(i=2;i<=a/2;i++)
{
    if(a % i == 0)
    {
        return 0;
    }
    else
    {
        continue;
    }
}
return 1; // loop has ended with no divisors --> prime!!
}

Or, better yet:

int primeornot(int a)
{
int i;

for(i=2;i<=a/2;i++)
{
    if(a % i == 0)
    {
        return 0;
    }
}
return 1; // loop has ended with no divisors --> prime!!
}



回答2:


Function primeornot returns immediately after the first test. You do not test every divisor up to a / 2 as you intend.

Note also that testing every number up to a / 2 is wasteful, you can stop when i * i > a.

Here is a corrected version:

int primeornot(int a) {
    int i;

    for (i = 2; i * i <= a; i++) {
        if (a % i == 0) {
            return 0;
        }
    }
    return 1;
}

You can further improve the function by testing 2 once and only odd numbers thereafter:

int primeornot(int a) {
    int i;

    if (a != 2 && a % 2 == 0)
        return 0;

    for (i = 3; i * i <= a; i += 2) {
        if (a % i == 0) {
            return 0;
        }
    }
    return 1;
}

Finally, the prototype for main without arguments is int main(void) and your should output a newline after the messages and return 0:

int main(void) {
    int number_given_by_user;

    printf("Enter a positive integer to find whether it is prime or not: ");
    scanf("%d", &number_given_by_user);

    if (primeornot(number_given_by_user)) {
        printf("The given number is a prime number\n");
    } else {
        printf("The given number is not a prime number\n");
    }
    return 0;
}



回答3:


You are returning if it is not divisible so the iteration will happen only once in for loop ! Because you are returning any way ! The below code will work for you !

#include<stdio.h>

int primeornot(int a)
{
    int i;
    for(i=2;i<=a/2;i++)
    {
        if(a % i == 0)
        {
            return 0;
            break;
        }

    }
    return 1;
}

int main()
{
    int number_given_by_user;

    printf("Enter a positive integer to find whether it is prime or not : ");
    scanf("%d",&number_given_by_user);

    if(primeornot(number_given_by_user))
    {
        printf("The given number is a prime number");
    }
    else
    {
        printf("The given number is not a prime number");
    }
}



回答4:


#include<stdio.h>

int primeornot(int a)
{
    int i, number_to_increment=0;

    for(i=1;i<=a;i++)
    {
        if(a % i == 0)
        {
            number_to_increment+=1;
        }
        else
        {
            number_to_increment+=0;
        }
    }
    if(number_to_increment==2)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

main()
{
    int number_given_by_user;

    printf("Enter a positive integer to find whether it is prime or not : ");
    scanf("%d",&number_given_by_user);

    if(primeornot(number_given_by_user))
    {
        printf("The given number is a prime number");
    }
    else
    {
        printf("The given number is not a prime number");
    }
}


来源:https://stackoverflow.com/questions/50549380/c-program-to-find-a-prime-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!