C Program to Check Prime Number

«
»

This is a program to check prime number in C.

Prime Number: A prime number is a number divisible only by 1 or itself.
Example: 2, 3, 5, 7, 11, 13, …., etc.

C Prime Number Problem Description

This program takes a number and checks whether a given number is prime or not.

advertisement
C Prime Number Problem Solution

1. Take a number as input.
2. Check if the number is divisible by any of the natural numbers starting from 2.
3. If it is, then it is not a prime number. Otherwise, it is a prime number.
4. Exit.

Prime Number C Program/Source Code

Here is the source code of the C program to check if a number is a prime number or not. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to find prime numbers and
  3.  * Output the given number with a suitable message.
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. void main()
  9. {
  10.     int num, j, flag;
  11.  
  12.     printf("Enter a number \n");
  13.     scanf("%d", &num);
  14.  
  15.     if (num <= 1)
  16.     {
  17.         printf("%d is not a prime numbers \n", num);
  18.         exit(1);
  19.     }
  20.     flag = 0;
  21. // To check prime number
  22.     for (j = 2; j <= num / 2; j++)
  23.     {
  24.         if ((num % j) == 0)
  25.         {
  26.             flag = 1;
  27.             break;
  28.         }
  29.     }
  30.     if (flag == 0)
  31.         printf("%d is a prime number \n", num);
  32.      else
  33.         printf("%d is not a prime number \n", num);
  34. }
Prime Number C Program Explanation

1. Take a number as input and store it in the variable num.
2. If the number is lesser than or equal to 1, then print the output as ” It is not a prime number”.
3. Initialize the variable flag to zero.
4. Using for loop, check if the input number is divisible by any of the natural numbers starting from 2.
5. If it is, then assigns the variable flag with 1.
6. Print the output as “It is a prime number”, if the variable flag ==0.
7. Otherwise print the output as “It is not a prime number” and exit.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
Prime Number in C Program Output
 
Enter a number
23
23 is a prime number
 
 
Enter a number
15
15 is not a prime number

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Take C Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.