# Thread: What is wrong with this program?

1. ## What is wrong with this program?

What is wrong with this program?

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

So i do this and here is my program to find a,b and c.

Code:
```#include <stdio.h>
main()
{
int a,b,c;

for (a=0;a<=1000;a++)
{
for (b=0;b<=1000;b++)
{
for (c=0;c<=1000;c++)
{
if (a+b==c&&((a*a)+(b*b)==(c*c))&&(a<b<c))
printf("%d,%d,%d",a,b,c);
}
}
}
getch();
}```

2. What makes you think that there is something wrong with your program?

3. it doesn't work...

4. As part of the process of helping you to diagnose and fix your own problems, tell us how exactly does it not work.

Is there a compile error? If so, what is the full error message? What do you understand by the error message?

Or, did the program compile but crash when run? If so, use your debugger to find the point of the crash and tell us where that is. The error would likely lie somewhere before that point.

Or, did you run the program, but it gave incorrect results? If so, what was the test input (none in this case), expected output and actual output?

5. There was no compiling error. The problem was that it gave incorrect results. The point of the program is to find a pythagorean triplet but in this case it got a large amount of 0's. so for example outputs were "0,1,1", "0,2,2" etc. until 1000 then a new form of output started: "1,0,1" "2,0,2" until 1,000. Then it ended.

6. So, a simple solution here is to start your loops from 1 rather than 0.

7. Well, Unfortunately, I tried that before and just now, and the program failed to execute

8. Then debug it.

Look, I gave you a recipe for asking for help. Follow it. Otherwise, my answers will be just as vague as your problem description.

9. Code:
`(a<b<c)`
You can't do compound inequalities like that in C. You must express each part separately and join them with an && or ||.

10. Right. That piece of code wasn't there the last time I checked. You should also warn people when you update your code, or better yet, post the updated code.

Furthermore, you don't need that check. Rather, you should start the outer loop from 1, but then you start the middle loop from a, and the inner loop from b.

11. its ok i just did using python but I'm still trying to fully understand C so I already knew how to do it

12. What is your current code?

Originally Posted by Simar Chhabra
its ok i just did using python but I'm still trying to fully understand C so I already knew how to do it
Out of curiosity, but what is your Python version? If you did this using Python for loops, I can expect that you might get a little stuck with the C version, but if you did this using Python while loops, the logic is likely to be exactly the same. Furthermore, the condition to test should be the same, but your C version looks wrong (the a+b==c check shouldn't be there, rather, you should be checking for a + b + c == 1000).

13. Code:
`if (a + b + c == 1000 && a * a + b * b == c * c && a < b && b < c)`

14. Simar, why do you have this in your condition? 'a+b==c'. compare that to your definition of a pythagorean triple.

15. You don't even need a loop for c. You're looping through 1000 values when you know up front that only one of them at most can be correct.

A little algebra knowledge allows one to turn this:
a + b + c == 1000
into this:
c == 1000 - a - b