In this post, we will write a C++ program to find out the prime numbers from a given range. user will enter the start and end number and in between that program will print the prime numbers.

C++ program to find out the prime numbers from a given range


C++ program to find out the prime numbers from a given range.

#include<iostream>
#include<conio.h>

int prime(int,int);

int main()
{
    system("cls");

    int st,en;

    std::cout<<"Enter the start value = ";
    std::cin>>st;
    std::cout<<"Enter the end value = ";
    std::cin>>en;

    prime(st,en);

    getch();
    return 0;
}

int prime(int st, int en)
{
    int i,j,count;

    for(i=st;i<=en;i++)
    {
        count = 0;

        for(j=1;j<=i;j++)
        {
            if(i%j == 0)
                count = count+1;
        }
        if(count == 2)
            std::cout<<i<<std::endl;
    }
}



Output

Enter the start value = 100
Enter the end value = 199
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199