Question

Write a function called productEven that takes as its parameter an input file. The function should...

Write a function called productEven that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function.


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

CODE:

#include <iostream>
#include <fstream>

using namespace std;

int produtEven(string filename);

int main() {
//calling the function
int output = produtEven("input.txt");
cout<<"Product returned by the function: "<<output<<endl;

}

int produtEven(string filename){
fstream file;
//opening both the input and output files
file.open(filename.c_str());
int a,b;
//getting the two numbers from the input file
file>>a;
file>>b;

int evenProd = 1;
for(int i=a;i<=b;i++){
//finding the product of all even numbers in the range
if(i%2 == 0){
evenProd *= i;
}
}
file.close();
//returning the product
return evenProd;
}

________________________________________

CODE IMAGES:

- jhyhm.cpp - Code::Blocks 17.12 File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Se

_____________________________________________

OUTPUT:

- х C:\Users\Win10\Documents\jhvhm.exe Product returned by the function: 645120 execution time : 0.263 s Process returned 0 (

input.txt:

input.txt - Notepad Х File Edit Format View Help 2 15

2 15

___________________________________________

Feel free to ask any questions in the comments section
Thank You!

Add a comment
Know the answer?
Add Answer to:
Write a function called productEven that takes as its parameter an input file. The function should...
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
  • please write program in C++ Write a function called productEven, that takes as its parameter an...

    please write program in C++ Write a function called productEven, that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function. please write the program in C++

  • Write a complete program that uses the functions listed below. Except for the printOdd function, main...

    Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates. •Write a...

  • Note: The order that these functions are listed, do not reflect the order that they should...

    Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all .cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function...

  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • Write a function in the C++called summary that takes as its parameters an input and output...

    Write a function in the C++called summary that takes as its parameters an input and output file. The function should read two integers find the sum of even numbers, the sum of odd numbers, and the cumulative product between two values lower and upper. The function should return the sum of even, odd, ad cumulative product between the lower and upper values. Please write program in C++.

  • •The multiplication operator works by summing some value, x, by another value, y. Write a function...

    •The multiplication operator works by summing some value, x, by another value, y. Write a function definition called multiply that takes two integer parameters to perform the operation described. Return the answer to the calling function. •Write a function called randCount with no parameters. The function should randomly generate 5 numbers between 8 to 15 and counts how many times a multiple of 2 occurred. Return the answer to the calling function. •Write a function called summary that takes as...

  • write in java split() A static method that takes as input parameter a ThingArrayQueue called inputQ,...

    write in java split() A static method that takes as input parameter a ThingArrayQueue called inputQ, that includes things with positive number attributes. The method returns an array of two ThingArrayQueues as output where the first queue includes all things with even values from input and the second queue includes all things with odd values from inputQ. Zero is considered an even number. The input queue should be empty after calling this method. *Queue has a mixture of things with...

  • Question 5 (25 points): Write a function even() that takes in as a parameter a file...

    Question 5 (25 points): Write a function even() that takes in as a parameter a file name. The file name contains a list of integers, one on each line but there may be bad data. The function will return a list of all of the even numbers found in the file and should not crash if it encounters non-numeric input. The function should catch all file access and conversion errors (ValueError). Upload your test file to D2L. >>> print (even...

  • 2. Write a function file_copy() that takes two string parameters: in file and out_file and copies...

    2. Write a function file_copy() that takes two string parameters: in file and out_file and copies the contents of in_file into out file. Assume that in_file exists before file_copy is called. For example, the following would be correct input and output. 1 >>> file_copy( created equal.txt', 'copy.txt) 2 >>> open fopen ( copy.txt') 3 >>> equal-f = open( 'created-equal . txt') 4 >>equal_f.read () 5 'We hold these truths to be self-evident, Inthat all men are created equalIn' 3. Write:...

  • Please write program in C++ Write a function called randCount with no parameters. The function should...

    Please write program in C++ Write a function called randCount with no parameters. The function should randomly generate 5 numbers between 8 to 15 and counts how many times a multiple of 2 occurred. Return the answer to the calling function. please write the program in C++

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
Active Questions
ADVERTISEMENT