1. ## Pythagorean Triples program

Hello,

I am writing a basic program in C whose function is to print all the Pythagorean Triples like these:

3,4,5

6,8,10

5,12,13

...

all the way up to 45.

This is my code:

Code:
```#include <stdio.h>

int main()
{
int LegOne, LegTwo, Hypotenuse;

Hypotenuse = 0;

while ( Hypotenuse < 50 )
{
LegTwo = 1;

while ( LegTwo < 50 )
{
LegOne = 1;

while ( LegOne < 50 )
{
if ( LegOne*LegOne + LegTwo*LegTwo == Hypotenuse*Hypotenuse && LegOne < LegTwo )
{
printf("\n\t\t The sides are %4d,%4d,%4d", LegOne,LegTwo,Hypotenuse);
}
LegOne++;
}
LegTwo++;
}
Hypotenuse++;
}

printf(\n\n);

}```
This code gives me this output:

As you can see, the numbers to the far left are not in order, they are scrambled all over the place. Is there a way to make the left numbers in ascending order without using "pow"? Thanks a lot.

2. But they are in order... they are in the exact order you calculated them in... If you want a different order, you can store your results in an array of structs, then sort the array any way you like. Alternatively you can cheat... change the order of the columns (note that the right column is in order)

BTW... yes you can do this with while() loops (obviusly) but I would think this would be a lot simpler using for() loops.

3. Originally Posted by CommonTater
But they are in order... they are in the exact order you calculated them in... If you want a different order, you can store your results in an array of structs, then sort the array any way you like.
I know they are in the order I calculated them in, but I don't know a way to get ascending numbers ie. 3,5,6,9...

Also I don't know what structs are, I am new to C and programming in general.

Originally Posted by CommonTater
Alternatively you can cheat... change the order of the columns (note that the right column is in order)
I was very tempted to do that but I want to learn how to do it the "right way"

Originally Posted by CommonTater
BTW... yes you can do this with while() loops (obviusly) but I would think this would be a lot simpler using for() loops.

Unfortunately for() is beyond my scope of learning, but one of my friends looked at my code and said it could be done solely with while().

4. Originally Posted by jamesallen4u
I was very tempted to do that but I want to learn how to do it the "right way"
Actually, in this case changing the display order to match the way your loops run would be the right way...

The hard way would be to build that array of structs and sort the thing... Take the easy way out...

Unfortunately for() is beyond my scope of learning, but one of my friends looked at my code and said it could be done solely with while().
Sounds to me like you need to sit down with a good book or tutorial on C (Google is your friend!) and undertake a deliberate study of the language. Any good text will present concepts in an order where you should be able to grasp them easily enough... just start on page 1, read and review until you understand, type up and compile all the exercises, play with the code, break it, fix it... find out what does and doesn't work... then move on to page 2... and so on till you run out of pages.

C is not something you can learn piecemeal... 'cause you will end up with situations like this one... on an every increasing scale. It will be the stuff you don't know that comes back to bite you.

5. Originally Posted by CommonTater
Sounds to me like you need to sit down with a good book or tutorial on C (Google is your friend!) and undertake a deliberate study of the language. Any good text will present concepts in an order where you should be able to grasp them easily enough... just start on page 1, read and review until you understand, type up and compile all the exercises, play with the code, break it, fix it... find out what does and doesn't work... then move on to page 2... and so on till you run out of pages.
^_^ You just described an efficient algorithm for learning C through book reading!!!

6. Originally Posted by jamesallen4u
I know they are in the order I calculated them in, but I don't know a way to get ascending numbers ie. 3,5,6,9...
They are in ascending order ... by the last column.

Quzah.

7. They are in ascending order ... by the last column because you while(Hypotenuse) first.
If you first loop LegOne then loop LegTwo then Hypotenuse), you will get the right order.

Code:
```LegOne = 1;
while(LegOne < 50)
{
LegTwo = 1;
while(LegTwo < 50)
{
Hypotenuse = 1;
while(Hypotenuse <50)
{
if (.....)
printf();
Hypotenuse ++;
}
LegTwo ++;
}
LegOne ++;
}```