I need the products of all ODD numbers under a user - inputted number. PLZ HELP

Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 #include <iostream>
#include <iomanip>
#include "cstdlib"

using namespace std;

int main()
{
double num, x, y;

cout << "Enter a whole number to determine the product of all odd numbers below: " << endl;
cin >> num;

x = 1;
y = 1;

while (x <= num)
{
 y = x * y;
 x = x + 2;
}

cout << setiosflags (ios :: fixed) << endl;
cout << " The product of all the odd numbers up to " << num << " IN DECIMAL FORM is: " << y << endl;

cout << setiosflags (ios :: scientific);
cout << endl;
cout << "The product of all odd numbers under " << num << " in SCIENTIFIC NOTATION is: " << y << endl;

system ("Pause");
return 0;
}
What is your question ? Looks like your code is working.
Last edited on
Your opening statement says "product of all odd numbers below: {number}"
But then one of your output prints says "all the odd numbers up to {number}"
So which is it?

If it's below, then you'd have the following test cases
1
2
3
4
5
6
7
8
9
10
10 -> 9 * 7 * 5 * 3 * 1
9 -> 7 * 5 * 3 * 1
8 -> 7 * 5 * 3 * 1
7 -> 5 * 3 * 1
6 -> 5 * 3 * 1
5 -> 3 * 1
4 -> 3 * 1
3 -> 1
2 -> 1
1 -> 0


So first, I'd change the x <= num to x < num.
You also will need to handle the case for num = 1 or 0, in which case y should equal 0.
1
2
if (num == 0 || num == 1)
    y = 0;

Write a C++ program that calculates and prints the product of all the odd numbers between 1 and a number entered by the user.

Ex. 30 (1*3*5*7*9*11*13*15*17*19*21*23*25*27*29)

Print the result in both decimal form (12.543) and scientific notation (1.2543e10)
Last edited on
Ex. 30 (1*3*5*7*9*11*13*15*17*19*21*23*25*27*29)


That does not produce a fraction, the output below is correct.

C:\>test
Enter a whole number to determine the product of all odd numbers below:
30

The product of all the odd numbers up to 30.000000 IN DECIMAL FORM is: 6190283353629375.000000

The product of all odd numbers under 30 in SCIENTIFIC NOTATION is: 6.19028e+015
Last edited on
Topic archived. No new replies allowed.