Question

Let's say I have n. n = 10.

I have to do P = 2 * 4 * 6 * 8 * n

So I can see that it's only even numbers that product together.

#include <iostream>

using namespace std;

int main()

{
int n, i, sn;

sn = 1;

cout << "Enter the number u will product to: \n";
cin >> n;

for (i = 1; i <= n; i++) {

if (n % 2 == 0);

sn += (i * i) ;


}

cout << sn;



return 0;
}


But the problem is guys that I don't get 3840 as a result. What's wrong?
Last edited on
Your if statement block has nothing in it.


if (n % 2 == 0);
In your if statement, the block of code that is repeated is whatever is between the ) (right after the zero in your line of code) and the ; . So it's empty.

Use braces.

1
2
3
4
if (n % 2 == 0) // NO SEMICOLON
{
  sn += (i * i) ;
}

n = 10.

Ok, variable "n" is set to the value 10.
s = 1;

Ok, you're setting a variable "s" to the value 1.
for (i = 0; i <= n; i++) {

Guessing you meant to add the missing word "int" before i. Ok, iterating by setting i to values 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
if (n % 2 == 0);

if "n" is even , nothing happens. Did you mean "i" ? And your erroneous semicolon ";" is the "nothing happens".
sum *= (i * i)

Ok, with each iteration of for loop, set sum to be the product of itself and the square of i (doesn't matter if i is even or not, since nothing happened there). Not sure why you want to square "i" -- did you mean a normal product? Also, the result of this will be 0 since we start at i=0. Not sure what "sum" is, since you had an "s" earlier.
cout << P;

Uh, what is P? You have "s", "sum", and "P" ? ;D

Hint: helps if you actually provide real code that compiles, preferably with [ code ] ... [ /code ] tags -- you'll spot errors much easier.

Working, compiling example with proper code tags:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main() 
{
    int n = 10;
    int p = 1;
    for (int i=2; i<=n; i+=2)
        p *= i;
    cout << p << endl;
    
    return 0;
}

3840
Last edited on
But I feel like my For line is not correct " for (i = 1; i <= n; i++)" or is it correct?
Please use coding format. Edit your post and add [code] and [/code] around your code.

(1.)
if (n % 2 == 0);
Do not put a semi-colon after an if-statement. Prefer curly braces:
1
2
3
4
if (n % 2 == 0)
{
    // do stuff in here
}


(2.) You are checking n to see if it's even. Shouldn't you be checking i?

(3.) sn += (i * i)
Why are you squaring i?

You should be multiplying your total by the next highest even number.

________________________________

This is a mistake a lot of beginners make: When testing your code, do not start at 10 or 100, or 7 trillion. Start at the simplest case you can think of. In your case, start at n = 2.
This should make the answer 2, right?
Then, try n = 3, then n = 4. You will start to see a pattern develop in your code that you can then generalize.

If you have never written code for the factorial function, I suggest looking that up.
Example: Finding the factorial of a number
https://www.programiz.com/cpp-programming/examples/factorial
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
    unsigned int n;
    unsigned long long factorial = 1;

    cout << "Enter a positive integer: ";
    cin >> n;

    for(int i = 1; i <=n; ++i)
    {
        factorial *= i;
    }

    cout << "Factorial of " << n << " = " << factorial;    
    return 0;
}
#include <iostream>

using namespace std;

int main()

{
int n, P;

P = 1;

cout << "Iveskite iki kiek dauginsite: \n";
cin >> n;

for (int i = 1; i <= n; i++)

if (i % 2 == 0)
{
P *= (i * i) ;
}






cout << P;



return 0;
}

This is the whole code that I have so far
DdavidDLT, ok that's pretty much right. Just for some reason you're squaring "i".
Also, just in case, make sure everything you want to repeat under the for loop is inside curly brackets { ... } . It's not necessary for single statements, but it can prevent mistakes.

(btw, you don't want to edit the first post too much, or future readers won't understand some of the comments. Just add updates in other posts)
Last edited on
Thank you all for the tips and hints I just figured out. Learned some things from this. Big thanks.
closed account (E0p9LyTq)
P *= (i * i); should be P *= i;.

PLEASE learn to use code tags, it makes reading your source MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

You can edit your posts and add code tags.
@Ganado, How did you put those "code tags" in the text without them being interpreted as code tags? I've been trying to do that. I tried the noparse tag, but it didn't work.
closed account (E0p9LyTq)
How did you put those "code tags" in the text without them being interpreted as code tags?

Wrap the tag you want shown with bold tags ([b] & [/b])
[code][/code]
I thought I tried that but only on the Preview. It didn't seem to work. I'll try it again right here:


EDIT: It doesn't work for me! How can that be?
Last edited on
Did you wrap each individual [code] [/code]piece in [b] [/b] tags ? :)
Last edited on
Here's what I copy/paste:
You can escape the [tt][/tt] with another tag of your choice. The literal text of this post is here:
https://pastebin.com/Y9BZS3AN

Place your source code within [co[tt][/tt]de][/co[tt][/tt]de] tags. For example, the input

[output][co[tt][/tt]de]
# include <iostream>
int main() { std::cout << "Hello, World!\n"; }
[/co[tt][/tt]de]
[/output]

Produces
[code]
# include <iostream>
int main() { std::cout << "Hello, World!\n"; }
[/code]

Last edited on
Topic archived. No new replies allowed.