Loading, please wait...

A to Z Full Forms and Acronyms

How to Print Prime Number in C Programming?

Aug 19, 2019 C Programming, 2107 Views
To print the prime number in a given range

Let's create a program to display prime no in a given range:

#include <stdio.h>
int main()
{
    int low, high, i, flag;
    printf("Enter two numbers: ");
    scanf("%d %d", &low, &high);
    printf("Prime numbers between %d and %d are: ", low, high);
    while (low < high)
    {
        flag = 0;
        for(i = 2; i <= low/2; ++i)
        {
            if(low % i == 0)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
            printf("%d ", low);
        ++low;
    }
    return 0;
}

Output:

Enter two numbers: 5 50
Prime numbers between 5 and 50 are: 5 7 11 13 17 19 23 29 31 37 41 43 47

Parameters used in the Program

  • #include<stdio.h> - It includes standard input output library functions. 
  • int main() - The main function is the entry point of every program in c.
  • printf() - The printf() function is used to print the data on the console.
A to Z Full Forms and Acronyms

Related Article