user9520489
user9520489

Reputation:

C program to find a prime number

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");
    }
}

Upvotes: 1

Views: 6883

Answers (5)

future
future

Reputation: 1

This program takes an integer as input, checks its primality, and prints the result. You can enhance this further to find all primes within a range or improve efficiency for larger numbers.

#include <stdio.h>;
#include <math.h>;

int main() {
  int num, isPrime = 1;
  printf("Enter a positive integer: ");
  scanf("%d", &num);

  // Handle non-positive numbers
  if (num &lt;= 1) {
    isPrime = 0;
  } else {
    // Check for divisibility from 2 to sqrt(num)
    for (int i = 2; i &lt;= sqrt(num); i++) {
      if (num % i == 0) {
        isPrime = 0;
        break;
      }
    }
  }

  if (isPrime) {
    printf("%d is a prime number.\n", num);
  } else {
    printf("%d is not a prime number.\n", num);
  }

  return 0;
}

Upvotes: -2

chqrlie
chqrlie

Reputation: 144740

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;
}

Upvotes: 2

Faustin
Faustin

Reputation: 1

#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");
    }
}

Upvotes: -2

CIsForCookies
CIsForCookies

Reputation: 12817

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!!
}

Upvotes: 1

Bharadwaj
Bharadwaj

Reputation: 11

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");
    }
}

Upvotes: 0

Related Questions