Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Use Python)Write a program on python that prompts the user to enter two positiv

ID: 674856 • Letter: #

Question

(Use Python)Write a program on python that prompts the user to enter two positive numbers. Only accept positive numbers - if the user supplies a negative number or zero or anything non-numeric you should re-prompt them.

Next, output all the prime numbers between the two numbers the user entered. A prime number is a number that has no positive divisors other than 1 and itself. For example, 5 is prime because the only numbers that evenly divide into 5 are 1 and 5. 6, however, is not prime because 1, 2, 3 and 6 are all divisors of 6.

Here's a sample running of your program:

Some notes on your program:

1 is technically not a prime number

Big hint: you cannot determine if a number is prime until you look at ALL potential divisors. Therefore you can't report to to the user that a number is prime within your loop structure.

You need to ensure that the start and end numbers are both positive numbers.

You also need to ensure that the start number is less than the end number

Explanation / Answer

#include<iostream>
using namespace std;

void print_prime(int n1,int n2)//The function to print the prime numbers in given range
{
   cout<<"The prime numbers between "<<n1<<" and "<<n2<<" are : ";
   for(int i=n1;i<=n2;i++)
   {
   int flag=0;
   for(int j=2;j<=i/2;j++)
   {
      if(i%j==0)
      {
      flag=1;
       break;  
       }
   }
   if(flag==0&&i!=1)
   {
      cout<<i<<" ";
   }
   }
}
int main()
{
   int n1,n2;
   char ch;
   while(true) //Loop to check if input priovided is correct or not,prompts to re-enter till correct input is fed
   {
     cout<<" Enter a start and end number, separated by a comma : ";
   cin>>n1>>ch>>n2;
   if(n1<=0||n2<=0)
      cout<<" Sorry, the start and end must be positive(>0)";
   else if(ch!=',')
      cout<<" Invalid input,please try again using a comma in between the numbers!";
   else if(n1>=n2)
      cout<<" End number must be greater than start number";
   else
   {
      print_prime(n1,n2);
      break;
   }
    }
}