C Program For Prime Numbers – Check Whether A Number Is Prime Or Not

C Program For Prime Numbers – If you are looking for finding prime numbers in C program, this article will guide you to learn to check whether a number is prime or not in C. Just go through this C program to find prime number tutorial you will be able to write a program to check prime number in C.

CodingCompiler.com publishing series of C programs for beginners, students, B.Tech graduates to enhance their C programming skills and hands-on experience on coding. This is the third C programming example in the series, it helps newbies in reaching their goals in the career. All the best guys in learning c programming with coding compiler blog.

C Program For Prime Numbers

Check whether a number is prime or not in C in C programming language can be done using various techniques, but here in this program, we used three variables to find prime numbers.

C Program To Find Prime Number Source Code

Copy paste the below C program to find prime number source code or write your own logic into C compilers and run the program to see the result.

Source Code:

/*C PROGRAM FOR PRIME NUMBER - PRIMENUMBERS.C */

#include <stdio.h>
int main()
{
   int n, i, flag = 0; //Variables

   printf("Enter a positive integer: "); 
   scanf("%d",&n); //Reading input from users

  for(i=2; i<=n/2; ++i) // Logic for finding prime number
  {
    
    if(n%i==0)
     {
      flag=1;
      break;
     }
  }

   if (flag==0)
     printf("%d is a prime number.",n);
   else
     printf("%d is not a prime number.",n);
 
 return 0;
}

C Program For Prime Numbers Output

After you compile and run the above program, your C compiler asks you to enter a number to check whether a number is a prime number or not. After you enter a positive integer, then it will show output with the result saying the entered number is prime or not like below expected output.

Enter a positive integer: 29

29 is a prime number.

Related C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

Leave a Comment