C - What is wrong with my logic?

I am supposed to write a program that outputs Pythagorean Triples up to a specific range entered by the use. The problem with my code is that it is not complete, for example if you input a range of 15 it is supposed to output:
(3, 4, 5)
(5, 12, 13)
(6, 8, 10)

but my program out puts nothing. It however does print out values for very high range values. What am I missing here?

Thank you in advance.

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
33
34
35
36
37
38
39
40
  #include <stdio.h>
#include<math.h>

int main(){

int range=0,
        PTN=0, // counter 
            a=1, b=2, c=3,
                max_a=0,max_b=0,max_c=0;

printf("Please enter a positive integer as the range: ");
scanf("%d",&range);


for(c=1 ; c < range ; c++){

    for(b=1; b < c; b++){

        for(a=1; a < b; a++){

            if( (pow(a,2) + pow(b,2) )== pow(c,2) ){
                printf("\n(%d,%d,%d)", a,b,c);
                PTN++; //counter

                if(max_a < a) max_a = a;
                    if(max_b < b) max_b = b;
                        if(max_c < c) max_c = c;
            }
        }
        a=1;
    }
    b=1;
}

    printf("\nThere were %d Pythagorean Triples of whom (%d,%d,%d) is the greatest.\n",PTN,max_a,max_b,max_c);

return 0;
}

Last edited on
The online C++ shell compiles and runs the program nicely.

pow() returns double. Equality comparison with floating point values is not trivial. You could have ( (a*a + b*b) == (c*c) ) and it would stay integral and exact all the time.

Your "greatest triplet" is logically wrong though; each member is potentially from a different triplet.
Oh I had no idea that pow returns double. Thank you very much!! And yes, I see what you mean with my logic error. I will fix it.

Again thank you for your time and help!
Topic archived. No new replies allowed.