40) Write a program to print prime numbers within a given range in C++.
In this we are going to see a Program to print Prime numbers in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

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

void main()
{
clrscr();
int i, j, n, c;
cout<<"Enter the upper limit of the range: ";
cin>>n;
cout<<"Prime numbers till "<<n<<" are: ";
for(i=2; i<=n; i++)
{
	c=0;
    for(j=1; j<i; j++)
        {
            if(i%j==0)
            c++;
        }
    if(c==1)
    cout<<i<<" ";
}
getch();
}

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

int main()
{
int i, j, n, c;
cout<<"Enter the upper limit of the range: ";
cin>>n;
cout<<"Prime numbers till "<<n<<" are: ";
for(i=2; i<=n; i++)
{
	c=0;
    for(j=1; j<i; j++)
        {
            if(i%j==0)
            c++;
        }
    if(c==1)
    cout<<i<<" ";
}
return 0;
}



#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post