In this segment you will be familiarize with prime numbers along with a program which determines whether the entered number is a prime or not a prime number.

## Prime numbers in C :

A prime numbers in C language can be defined as the natural numbers ( i.e. 1 to infinite ) which are divisible by 1 and the number itself.

For example :

17 which is divisible by 1 and by 17 i.e. number itself.

### Some important point about prime numbers ###

# An even number can’t be a prime number because all even numbers are divisible by 2.

For example :

148 is not a prime number but 148 is even number that is, it is divisible by 2.

# All numbers in which sum of all digits is divisible by 3 are not prime numbers.

For example :

480 is not a prime no. as 480 is divisible by 3.

# All numbers having 5 at unit place are divisible by 5 that why numbers ending with 5 are not prime number.

For example :

625 is not a prime no. as 625 is divisible by 5.

# 0 and 1 are not considered as prime numbers.

#### Prime numbers from 1 to 100 are given in a table below :

2 | 3 | 5 | 7 | 11 |
---|---|---|---|---|

13 | 17 | 19 | 23 | 29 |

31 | 37 | 41 | 43 | 47 |

53 | 59 | 61 | 67 | 71 |

73 | 79 | 83 | 89 | 97 |

#### Program to check whether the entered number is a prime number or not

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> int main() { int n, i, flag = 0; printf("Enter a positive integer... "); scanf("%d",&n);\ for(i=2; i<=n/2; ++i) { // condition for nonprime 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; } |

OUTPUT of program given above:

1 2 3 4 |
Enter a positive integer... 47 47 is a prime number. |

#### Program to check whether the entered number is a prime number or not using functions:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <stdio.h> int prime(int); int main() { int n,result; printf("Enter a positive integer... "); scanf("%d",&n); result = prime(n); if (result==0) printf("%d is a prime number.",n); else printf("%d is not a prime number.",n); return 0; } int prime(int num) { int i, flag = 0; for(i=2; i<=n/2; ++i) { // condition for non prime number if(n%i==0) { flag=1; break; } } return flag; } |

When code given above is compiled and executed following result will be obtained.

OUTPUT of program given above:

1 2 3 4 |
Enter a positive integer... 156 156 is not a prime number. |

This is all about prime numbers along with the program which determines whether the entered number is a prime or not a prime number.

If you have any query please comment below.