» » » C program to print prime numbers 1 to n

# C program to print prime numbers 1 to n

• Any number which is divisible by 1 and itself is known as prime number.
• To check prime number or not in c programming we need to use for loop and iterate from 2 to half of the number.
• If any number is divisible then it is non prime number, we can exit the loop.
• Now we need to check prime numbers from 1 to 100 or 1 to n.
• Logic is same and we are adding one more for loop for 1 to N numbers.
• Now we will iterate for loop from 1 to n and check prime or nor for each number.
• Lets see an example program on c to check prime numbers from 1 to N.

Program #1 : Write a c program to check prime numbers from 1 to N by using for loop.

1. #include <stdio.h>
2. int main()
3. {
4.    int n, i,j, count = 0;
5.
6.     printf("Enter a Number");
7.     scanf("%d",&n);
8. for(i=2;i<=n;i++)
9.  {
10.   for(j=2;j<i;j++)
11.   {
12.    if(i%j==0)
13.    break;
14.    else if(i==j+1)
15.    printf("%d\n",i);
16. }
17. }
18.
19.     getch();
20.
21. }

Output:

You might like: