C program to print prime numbers till N is to print the possible prime numbers from 1 to N.

What is prime number?
Any number which is divisible only by one and itself. two (2) is the smallest even prime number.
Example: 2, 3, 5, 7, 11, 13, 17,…..
A number greater than one can be factorized into prime numbers, for example, 540 = 22*33*51

Algorithm to print prime number

  1. Take a input number from a user. N
  2. Loop i = 2 t0 N
    If i %2 == 0 And i divisible by itself
    Print prime number i
    else
    Continue

C program to print prime numbers till N

#include<stdio.h>

int main()
{
int n, i = 3, count, c;

printf("Enter the number of prime numbers required\n");
scanf("%d",&n);

if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}

for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n", i);
count++;
}
i++;
}

return 0;
}

Output :

C program to print prime numbers till N

Write A Comment