The prime number program in C is one of the most important programs in c.
What is a prime number?
Any number which is divisible only by one and itself. two (2) is the smallest even prime number. for example prime numbers are: 2, 3, 5, 7, 11, 13, 17,…..
A number is greater than one can be factorized into prime numbers, for example, 540 = 22*33*51.
Algorithm for Prime number program in C
- Take the input number. no
- Set c=2
- Write loop for ( c = 2 ; c <= n – 1 ; c++ )
if n%c == 0
Print No is not a prime number
break; - If c == n
Print No is a prime number
C Program to Check Whether a Number is Prime or not
#include<stdio.h> main() { int n, c = 2; printf("Enter a number to check if it is prime\n"); scanf("%d",&n); for ( c = 2 ; c <= n - 1 ; c++ ) { if ( n%c == 0 ) { printf("%d isn't prime.\n", n); break; } } if ( c == n ) printf("%d is prime.\n", n); return 0; }
Output :