Answer to Question #157606 in C++ for Usama Saleem

Question #157606

Write a function which takes as argument two integers and prints all the prime numbers between those two numbers. If the first number is smaller than the second, the prime numbers would be displayed in an increasing order. If the first number is bigger than the second, the prime numbers would be displayed in an decreasing order.


1
Expert's answer
2021-01-22T15:03:44-0500
#include <iostream>

using namespace std;

int isPrime(int n)
{
int count=0;
for(int i=2;i<(n/2)+1;i++)
{
if(n%i==0)
count++;
}
if(count==0)
return 1;
else
return 0;
}

void prime(int a, int b)
{
cout<<"\n";
if(b>a)
{
for(int i=a;i<=b;i++)
{
if(isPrime(i)==1)
cout<<i<<" ";
}
}
else
{
for(int i=b;i<=a;i++)
{
if(isPrime(i)==1)
cout<<i<<" ";
}
}
}

int main()
{
prime(10,20);
prime(20,10);
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog