# Thread: having problem with a simple loop

1. ## having problem with a simple loop

Hello All,
My instructor gave me a question to solve , its really simple but it keeps displaying me 0
the questions says :
Write a C program to Calculate and display the product of the even integers from 2 to 50.

here is my work and i dunno why it displays 0.
Code:
```#include <stdio.h>
#include <conio.h>
main()
{
int i=2,P;
while(i<50)
{
i=i+2;
P=P*i;
}
printf("the product is %d",P);
getch();
}```

2. You didn't initialize P - right now, P just happens to be whatever is in memory when you declared it, and that value happened to be 0 here.

3. The product of even integers from 2 to 50 is 2^25 * 25! I believe which FAR exceeds the capacity of an integer. 2^20 itself is over 1 million! You may want to consider approximating that value or using some larger primitive type.

4. mm kk i corrected it and made it double instead of int but idk why the instructor put it in int if it wont be able to work that way ? weired , anyways thanks for the help, appreciate it

5. From Claudiu's calculation: 2 ^ 25 * 25 = 838 860 800
The upper range of a signed integer on most 32 bit machines: 2 ^ 31 - 1 = 2 147 483 647

It should fit.

6. 25! not just 25.

Think about it. There are 25 even numbers. If you extract the 2 factor from each one you end up with 2^25. The rest is the product of 1*2*3...*25.

7. Oh lol, my bad. I thought that was just enthusiasm or awe.

Popular pages Recent additions