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

Consider an input file A2.txt. Each line of A2.txt consists of two numbers separ

ID: 3851012 • Letter: C

Question

Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and prints how many numbers are prime between them. Your program must have a user defined function that takes one number as input parameter and detects whether its prime or not. Show your output in a file named B2.txt using the following format: Sample input: 3 17 42 91 Sample output: 6 11 (For further clarification, the first line of the sample output = 6 because from 3 to 17 there are 6 prime numbers which are 3, 5, 7, 11, 13, 17)

Explanation / Answer

#include <iostream>
using namespace std;

int checkPrimeNumber(int);

int main()
{
int num1, num2;
bool flag;

cout << "Enter two positive integers: ";
cin >> num1 >> num2;

  
   int count=0;
for(int i=num1+1;i<num2;++i)
{
// If i is a prime number, flag will be equal to 1
flag = checkPrimeNumber(i);
if(flag == true)
{
           count++;       //Count will be increased if a current number is prime number
       }
}
cout << count;
return 0;
}

// user-defined function to check prime number
int checkPrimeNumber(int n)
{
bool flag = true;

for(int j=2;j<=n/2;++j)
{
if (n%j==0)
{
flag = false;
break;
}
}
return flag;
}

Ouput: 3 17

Number of prime numbers between 3 17 is= 4

Prime number are=5 7 11 13(excluding 3 and 17)