Write a program that reads the values from a file and writes

Write a program that reads the values from a file and writes the sum of all multiples of 2 or 5 into another file. You can use the similar logic from the last recitation. Submit a zip file containing your .cpp program and the output text file under the link Recitation 6 Submit. Your program must contain separate functions for reading and writing a file. Submissions are due Sunday 5pm.

Solution

Here is the program as per your requiremensts.
#include <iostream.h> // must include to run a c++ file.
#include <fstream.h> // must for filesteam to deal with disk files.
#include <string.h> // string functions to be handled
#include <stdlib.h> // standard library functions.
int main () // main function from where execution starts
{
char ns[20]; //character array or string variable declaration.
int n; // declaration of integer variable
long int sum =0; // declaring and initializing
ifstream inputfile (\"Input.txt\"); // opening input file named input.txt into the variable inputfile
ofstream outputfile(\"Output.txt\"); // opening output file named output.txt into the variable outputfile.
if(inputfile != NULL) // if the input file is opened
{
while ( !inputfile.eof() ) // iterate through the input file.
{
   inputfile >> ns;
   n = atoi(ns);
   if(n % 2 == 0 || n % 5 == 0)
   sum = sum + n;
}
if(outputfile != NULL) // if the output file is opened
{
   outputfile << sum ;
}
else
   {
   cout << \"\ Unable to open the output file.\" ;
   }
}
else
{
   cout << \"\ Unable to open the input file.\";
}
inputfile.close();
outputfile.close();
return 0;
}
Input:
4
6
5
2
10
25
Output:
52

 Write a program that reads the values from a file and writes the sum of all multiples of 2 or 5 into another file. You can use the similar logic from the last

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site