Question

Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space....

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)

I realize there is already another answer to this question on Chegg, however there are multiple ways in which the code can be written but I was hoping someone could provide a easier, understandable and more basic code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM

#include<iostream>
#include<fstream>
using namespace std;
//prime method
bool isPrime(int n)
{
    int i,c=0; //c is a variable count the number of factors
    //loop from 2 to n-1
    for(i=2;i<=n-1;i++)
    if(n%i==0) //if divisible then i is a factor
    {
        c++; //count the number of factors
        break; //terminate the loop
       }
    if(c==0) //if c==0 then its a prime number
    return true;
    else
    return false; //return false when its not a prime number
}

int main()
{
   //declare the input file object
   ifstream infile;
   //declare the output file object
   ofstream outfile;
   //variable declarations
   int a,b,i,count=0;
   //open the input file
   infile.open("D:/A2.txt");
   //if unable to open the file then display the messge
   if(!infile)
{
cout<<endl<<"Unable to open the source file";
exit(0);
}
//open the file for writing purpose
outfile.open("D:/B2.txt");
//block when unable to open the file for writing purpose
if(!outfile)
{
cout<<endl<<"Unable to open the target file";
infile.close();
exit(0);
}
//loop to read the data from input file
   while(!infile.eof())
   {
      count=0;//set the count to 0
      infile>>a>>b;//Read the data from file and store them in two variables
      for(i=a;i<=b;i++) //loop from starting value to ending value
          if(isPrime(i)) //check for prime or not
              count++; //count when the number is prime
       outfile<<count<<" "; //write the count into the file
   }
}

Output

Input file

Out file

Add a comment
Know the answer?
Add Answer to:
Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider an input file A1.txt. Each line of A1.txt consists of two numbers separated by space....

    Consider an input file A1.txt. Each line of A1.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and adds all the integers between those two numbers (starting from the first number and ending at the second. Show your output in B1.txt. Sample Input: 1 6 18 74 29 38 Sample Output: 21 2622 335 (For further clarification, the first line of the sample output was done by 1+2+3+4+5+6 =...

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files...

    IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files for the following Knapsack Problem that you will solve. KnapsackData1.txt contains a list of four prospective projects for the upcoming year for a particular company: Project0 6 30 Project1 3 14 Project2 4 16 Project3 2 9 Each line in the file provides three pieces of information: 1) String: The name of the project; 2) Integer: The amount of employee labor that will be demanded by the...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Assume that you will be given a *.txt file, which in the same format of RandSource.txt...

    Assume that you will be given a *.txt file, which in the same format of RandSource.txt (attached). The file has three numbers at each line. Numbers are separated with a space. All number are in the range of [0, 50]. The first two numbers indicate a range. The third one shows number of random numbers to be generated in the range of first two numbers. The range always given in first < second order. If not, raise an error. For...

  • IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files for the following...

    IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files for the following Knapsack Problem that you will solve. KnapsackData1.txt contains a list of four prospective projects for the upcoming year for a particular company: Project0 6 30 Project1 3 14 Project2 4 16 Project3 2 9 Each line in the file provides three pieces of information: 1) String: The name of the project; 2) Integer: The amount of employee labor that will be demanded by the...

  • 1. Write a program called Numbers that a. prompts the user for a file name. b....

    1. Write a program called Numbers that a. prompts the user for a file name. b. reads that file assuming that its contents consist entirely of integers. c. prints the maximum, minimum, sum, count (number of integers in the file), and average of the numbers. For example, if the file numberinput.dat has the following content: 4 -2 18 15 31 27 Your program should produce the following output: csc% java Numbers Enter file name: numberinput.daft Maximum31 Minimum- -2 Sum -...

  • Please provide C language code no c++ ,txt file also needed with comment Finish task 5...

    Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) ยท Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...

  • Overview These exercises will allow you to have some practice with basic Java file Input/Output. In...

    Overview These exercises will allow you to have some practice with basic Java file Input/Output. In addition, you will also have an opportunity to have more practice with methods and arrays.   Objectives Practice with programming fundamentals Variables - Declaration and Assignment Primitive types Arithmetic Expressions Simple keyboard input and text display output Branching - if-elseif-else syntax Loops - simple while loops, nested while loops Methods - functions and procedures ArrayLists - collections of variables File I/O Works towards the following...

  • Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a...

    Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a function named, file_commands, that takes the name of a file as a parameter. The function processes the contents the file as follows: For file lines that begin with the letter 'a', calculate & print the integer average of the numbers on the line. For file lines that begin with the letter 'u', print the upper case format for each word following the u. Sample...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT