Write a program that uses a forstatement to calculate and print the product of t
ID: 3617156 • Letter: W
Question
Write a program that uses a forstatement to calculate and print the product of the oddintegers from1 to 15. Also make sure the program can also read any othernumbers for example (0 to 8 or100) Please Explain and elaborate on all yoursteps. Thanks Write a program that uses a forstatement to calculate and print the product of the oddintegers from
1 to 15. Also make sure the program can also read any othernumbers for example (0 to 8 or100) Please Explain and elaborate on all yoursteps. Thanks
Explanation / Answer
// Multiply odds // March 2nd 2010 // Created By Nic Raboy#include <iostream>
using namespace std;
int main() { int count; // Declare a loop count variable int product = 1; // Start at 1 because 0 would make the finalanswer 0 cout << "How many times would you like to loop?>"; cin >> count; // What you will loop until // Loop until the number you inputed for(int i = 0; i <= count; i++) { // Use modulus to see if the number has a remainder whendividing by // two. If there is a remainder then it is odd if(i%2 == 1) { cout << i << " "; // Print out all the odd numbersused product *= i; // Get a running product of all thenumbers } } cout << endl; // Make a new line to separate all the oddnumbers cout << "The product is: " << product <<endl; // Print out your product return 0; } ------------------------------------------ This code should be what you want. I wasn't sure whatyou meant by reading in other numbers so I assumed you wanted to beable to enter the number to loop until. If this is not whatyour looking for let me know.
I commented the code. If you are not sure what ishappening still let me know.
Compiled and tested with G++
Nic Raboy
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.