### What is a prime number

*Prime number* is a natural number which is greater than 1.

A number which is divisible by 1 and itself only is called as **prime number**.

For example, 5 is *Prime number* because 1 and 5 are its only integer factors.

Whereas 6 is not *Prime number* because it has the divisors 2 and 3.

A number which is not *Prime number* is called as **composite number**.

### Program to determine given number is prime or not

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> int main() { int num, i, flag = 0; printf("Enter a number "); scanf("%d",&num); for(i=2; i<=num/2; ++i) { if(num%i==0) { flag = 1; break; } } if (flag == 0) printf("%d is a prime number.", num); else printf("%d is not a prime number.", num); return 0; } |

**Output:**

1 2 3 |
Enter a number 51 51 is not a prime number. |

1 2 3 |
Enter a number 11 11 is a prime number. |

### Program to get all prime numbers upto given input number

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 i, j, num=0; int f = 1; printf("Enter a numer\n"); scanf("%d\n",&num); printf("List of all prime numbers up to %d is\n", num); for(i=2; i<=num; i++) { f = 1; for(j=2; j<i; j++) { if(i % j == 0) { f = 0; break; } } if(f != 0) { printf("%d\t", i); } } } |

**Output**

1 2 3 4 |
Enter a numer 44 List of all prime numbers up to 44 is 2 3 5 7 11 13 17 19 23 29 31 37 41 43 |

### Program to get all prime numbers between given input numbers

Write a program which take two numbers from user and print all prime numbers between these numbers.

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 33 34 35 36 37 38 |
#include<stdio.h> int main() { int i, j, num1, num2; int f = 1, temp; printf("Enter a first number\t"); scanf("%d\n",&num1); printf("Enter a second number\t"); scanf("%d\n",&num2); printf("List of all prime numbers between %d and %d is\n", num1, num2); //to check first number is smaller than second number if(num1 < num2) { } //if first number is larger than second than exchange values else { temp = num1; num1 = num2; num2 = temp; } for(i=num1; i<num2; i++) { f = 1; for(j=2; j<i; j++) { if(i % j == 0) { f = 0; break; } } if(f != 0) { printf("%d\t", i); } } } |

**Output**

1 2 3 4 |
Enter a first number 12 Eter a second number 44 List of all prime numbers between 12 and 44 is 13 17 19 23 29 31 37 41 43 |

1 2 3 4 |
Enter a first number 44 Eter a second number 12 List of all prime numbers between 44 and 12 is 13 17 19 23 29 31 37 41 43 |