C program to print all prime numbers between 1 to n – All those who are searching for Prime Number Program in C can check it out all the details here. This code prints prime numbers using c programming language. Any whole number which is greater than 1 and has only two factors that is 1 and the number itself, is called a prime number. Here is source code of the C program to check if a given number is prime. The C program is successfully compiled and run on a Linux system. Let’s see the prime number program in C. In this c program, we will take an input from the user and check whether the number is prime or not.

Read More – C Palindrome Program, Factorial Program in C

**Example:**

Input lower limit: 1

Input upper limit: 10

Output prime numbers between 1-10: 2, 3, 5, 7

## Program to print prime numbers between 1 to n

`/**`

` `

`* C program to print all prime numbers between 1 to n`

` `

`*/`

`#include <stdio.h>`

`int`

`main()`

`{`

` `

`int`

`i, j, n, isPrime; `

`//isPrime is used as flag variable`

` `

`/* Reads upper limit to print prime */`

` `

`printf`

`(`

`"Find prime numbers between 1 to : "`

`);`

` `

`scanf`

`(`

`"%d"`

`, &n);`

` `

`printf`

`(`

`"\nAll prime numbers between 1 to %d are:\n"`

`, n);`

` `

`/* Finds all Prime numbers between 1 to n */`

` `

`for`

`(i=2; i<=n; i++)`

` `

`{`

` `

`/* Assume that the current number is Prime */`

` `

`isPrime = 1;`

` `

`/* Check if the current number i is prime or not */`

` `

`for`

`(j=2; j<=i/2; j++)`

` `

`{`

` `

`/*`

` `

`* If i is divisible by any number other than 1 and self`

` `

`* then it is not prime number`

` `

`*/`

` `

`if`

`(i%j==0)`

` `

`{`

` `

`isPrime = 0;`

` `

`break`

`;`

` `

`}`

` `

`}`

` `

`/* If the number is prime then print */`

` `

`if`

`(isPrime==1)`

` `

`{`

` `

`printf`

`(`

`"%d is Prime number\n"`

`, i);`

` `

`}`

` `

`}`

` `

`return`

`0;`

`}`

So, above code is totally free of error you can easily use this for C program to print all prime numbers between 1 to n, Prime Number Program in C. Above we have shared each and everything about Prime Number Program in C. If you have any query or problem connect with us. Comment on below box for any query.

## Leave a Reply