# C program to print prime numbers between two numbers

Prime numbers are the numbers which have only two factors, 1 and the number itself. That means, they are not divisible by any number other than one and the number itself.

In this article, we’ll learn how to check whether a number is prime or not, with and without using function. But before that, we’ll write a program to print prime numbers from 1 to 200.

This C program prints prime numbers from 1 to 200. This algorithm can also be used to check whether a number is prime or not.

## Program: C program to print prime numbers from 1 to 200

```#include<stdio.h>
int main()
{
int n,i,flag;
for(n=1; n<=200; n++)   /*iterate over numbers from 1 to 200*/
{
flag=0;
for(i=2;i<=(n/2);i++)
{
if((n%i)==0)         /* check prime or not*/
{
flag=1;
break;
}

}
if(flag==0)
printf("%d ",n);
}
printf("\n");
return 0;
}```

Output: Prime numbers from 1 to 200

``1 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 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199``

Now, we’ll write a program to check whether a number is prime or not using function. The same algorithm is used to serve this purpose.

## Program: C program to check whether a number is prime or not using function

```#include<stdio.h>
int checkPrime(int);
int main()
{
int n,i,flag;
printf("Enter a number: ");
scanf("%d", &n);
flag = checkPrime(n);
if(flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
printf("\n");
return 0;
}
int checkPrime(int a){
int i;
for(i=2;i<=(a/2);i++)
{
if((a%i)==0)         /* check prime or not*/
return 1;
}
return 0;
}```

Output:

``````Sample output #1:

Enter a number: 4337
4337 is a prime number.

Sample output #2:

Enter a number: 4239
4239 is not a prime number.``````

Challenge: Write C program to check whether a number is prime or not in the comments.

In this article, we learnt to write program in C to check prime numbers and to print prime numbers between two numbers.

For more programming articles and code snippets, visit programming articles. 