C Program to Print Prime Numbers up to a Given Number

In this tutorial, you will learn how to display all the Prime Numbers from 1 to entered Range in C.

Prime Number:

A Prime Number is a number that is only divisible by 1 and itself. Example: 2, 3, 5, 7, 11, 13, 17, etc. These numbers are only divisible by 1 and the number itself.

The program takes the input from the user. It takes the upper range value and depending on this it will display all the Prime numbers between 1 to upper-value range.

You must know the following first:


C program to Display all the Prime Numbers up to a Given Number.

Source code:

#include <stdio.h>

void main()
{
  int num, i, j, temp;

 //User Input
  printf("Enter the Number upto which you want to print: ");
  scanf("%d", &num);

 //Printing all the Prime number
  printf("Prime Numbers upto %d are: \n", num);
  for (i = 1; i <= num; i++)
  {
    temp = 0;
    for (j = 1; j <= num; j++)
    {
      if (i % j == 0)
        temp++;
    }

    if (temp == 2)
      printf("%d ", i);//printing
  }

  getch();
}

The output of Prime Number in C programming.

You may go through the following prime program in C.