#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
// Declare the variables
int a, b, i, j, flag;
int t;//test cases
cin>>t;
for(int k=0;k<t;k++)
{
// Ask user to enter lower value of interval
cout << "Enter lower bound and upper bound of the interval: ";
cin >> a; // Take input
// Ask user to enter upper value of interval
// cout << "\nEnter upper bound of the interval: ";
cin >> b; // Take input
// Print display message
cout << "\nPrime numbers between "
<< a << " and " << b << " are:\n";
// Traverse each number in the interval
// with the help of for loop
for (i = a; i <= b; i++)
{
// Skip 0 and 1 as they are
// niether prime nor composite
if (i == 1 || i == 0)
continue;
// flag variable to tell
// if i is prime or not
flag = 1;
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
// flag = 1 means i is prime
// and flag = 0 means i is not prime
if (flag == 1)
cout << i <<"\n";
}
}
getch();
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
// Declare the variables
int a, b, i, j, flag;
int t;//test cases
cin>>t;
for(int k=0;k<t;k++)
{
// Ask user to enter lower value of interval
cout << "Enter lower bound and upper bound of the interval: ";
cin >> a; // Take input
// Ask user to enter upper value of interval
// cout << "\nEnter upper bound of the interval: ";
cin >> b; // Take input
// Print display message
cout << "\nPrime numbers between "
<< a << " and " << b << " are:\n";
// Traverse each number in the interval
// with the help of for loop
for (i = a; i <= b; i++)
{
// Skip 0 and 1 as they are
// niether prime nor composite
if (i == 1 || i == 0)
continue;
// flag variable to tell
// if i is prime or not
flag = 1;
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
// flag = 1 means i is prime
// and flag = 0 means i is not prime
if (flag == 1)
cout << i <<"\n";
}
}
getch();
return 0;
}
No comments:
Post a Comment