Find Prime Numbers in C Program | Quick and Easy Method

  • Thread starter Dili
  • Start date
In summary, there are several errors in the code that need to be corrected in order to properly check for prime numbers. These include incorrect use of braces, not checking for divisibility by 2, and not including the square root of the number in the loop. It is recommended to use a debugger to trace the execution of the code and identify the errors.
  • #1
Dili
13
0
Ive wrote a C program to find out whether a number is prime. But it doesn't work on some numbers, eg 25
Any help, ideas are most welcome

#include<stdio.h>
#include<math.h>
int main()
{
int j,number;
scanf("%d",&number);
if(number<=1)
return 0;
else if(number==2)
return 1;
else
for(j=3;j<(int)sqrt(number);j+=2)
{
if(number%j==0)
printf("not prime\n");
else
printf("prime\n");
return 1;
}
return 0;
}
 
Technology news on Phys.org
  • #2
Again problem with loops. Within the for loop note that if the no. is not divisible by any j, then it prints prime, which is wrong. Instead you should use a flag of some sort to indicate that within the loop at least one instance of j has divided the no. There are other errors like j+=2, which should be done only if you have checked that the no. is not even earlier.
Can you make the corrections and post the revised code?
 
  • #3
You need to check for divisibility by 2, and you need to include the square root of the number.

Code:
if (number%2 == 0)
  return n == 2;
if (number <= 1)
  return false;
for (j = 3; j <= (int)sqrt(number); j += 2)
  if (number % j == 0)
    return false;
return true;

Faster:

Code:
int root;
if ((number&1) == 0)
  return n == 2;
if (number < 9)
  return number > 1;
root = (int)sqrt(number);
for (j = 3; j <= root; j += 2)
  if (number % j == 0)
    return false;
return true;
 
  • #4
prime

i modified the code. but there is an error.
After 55 every number incremented by 10 is said as prime\
(eg; the program says that 55,65,75... are primes)
can anyone have any idea?
furthur how do you modify the code so that when 2 is given it is returned prime??

here is the code
#include<stdio.h>
#include<math.h>
int main()
{
int a,j,number;
scanf("%d",&number);
if(number<=1)
return 0;
if(number%2==0)
return 0;
for(j=3;j<=(int)sqrt(number);j+=2)
{
a=number%j;
}
if(a==0)
return 0;
else
printf("prime\n");

return 0;
}
 
  • #5
Your braces are wrong (the problem is not just with multiples of 5, if you go higher you'll find other composites are declared prime). If you want to handle 2 properly you'll have to replace the "return false" after your divisibility by 2 test with braces including an if statement checking if the number is two, and only then returning.
 
  • #6
i managed to correct 2 by putting anf if statement in the beginning
if(number==2);
printf("prime\n")

but can you tell where i should put the braces??

Thank you for all the help anyway. you posted a lot of replies to the questions
 
  • #7
You will find it enormously useful to learn how to trace the execution of your programs with a debugger. I don't know what you are currently using but integrated development tools like Visual Studio or Code::Blocks make this very easy to do (these two are free, by the way). Tracing your code will show you when the logic of your program goes wrong.

For this particular program the "for" loop does not contain any check for primality, which you put after the loop when it is too late. The braces are wrong is another way of saying that your prime check is not included within the loop (the check will also need to be corrected). Situations like this will be obvious when you execute your code line by line with a debugger, and their solution will also become more apparent.
 

Related to Find Prime Numbers in C Program | Quick and Easy Method

1. What is a C program for finding prime numbers?

A C program for finding prime numbers is a computer program written in the C programming language that is designed to identify and list all of the prime numbers within a given range or up to a specified limit.

2. How does a C program determine if a number is prime?

A C program determines if a number is prime by using a loop to check if the number is divisible by any numbers other than 1 and itself. If the number is only divisible by 1 and itself, it is considered a prime number.

3. Can a C program check if a large number is prime?

Yes, a C program can check if a large number is prime. However, as the number gets larger, the program may take longer to run and require more computational power.

4. How can I optimize a C program for finding prime numbers?

There are a few ways to optimize a C program for finding prime numbers, including using efficient algorithms such as the Sieve of Eratosthenes or implementing parallel processing. Additionally, avoiding unnecessary computations and minimizing the use of memory can also improve the program's efficiency.

5. Can a C program generate a list of prime numbers?

Yes, a C program can generate a list of prime numbers by using a loop to check each number within a given range and adding the prime numbers to a list or array. The program can then print or display the list of prime numbers for the user.

Similar threads

  • Programming and Computer Science
Replies
22
Views
969
  • Programming and Computer Science
Replies
4
Views
766
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
923
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
Back
Top