C Program to print all prime numbers between 1 to n


In this program, You will learn how to print all prime numbers between 1 to n in c.


Some list of prime numbers is : 2, 3, 5, 7, 11


Example: How to print all prime numbers between 1 to n in c

Run it Now
#include<stdio.h>

int main() {

   int n, i, j, p;
   printf("Enter upto Prime Numbers :");
   scanf("%d", &n);

   for (i = 2; i < n; i++) {
       p = 1;
       for (j = 2; j < i; j++) {
           if (i % j == 0) {
               p = 0;
               break;
           }
       }

       if (p == 1) {
           printf("\n%d", i);
       }
   }

   return 0;
}

Output:

Enter upto Prime Numbers :10
2
3
5
7