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.

#include<stdio.h>

int main() {

  int n, i, j, p;
  printf("Enter a number:");
  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 a number:15                                                                                                                      
                                                                                                                                       
2                                                                                                                                      
3                                                                                                                                      
5                                                                                                                                      
7                                                                                                                                      
11                                                                                                                                     
13