# nested for loops; while and if statements

Page 1 of 1

## 10 Replies - 2751 Views - Last Post: 22 February 2011 - 07:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=217338&amp;s=47ab6322b144eb18180ae0fa5b45a7e0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

# nested for loops; while and if statements

Posted 19 February 2011 - 07:47 PM

hey guys

I'm trying to figure out how to avoid printing out permutation of a Pythagorean triplet. Like I have the program all written but I am getting permutations of pythag triple.

Quote

A “Pythagorean triple” is three integers that are the sides of a right triangle, that is, the sum of the squares of the first two numbers is equal to the square of the third number. For example, (3, 4, 5) is a Pythagorean triple. Write a program that will list all Pythagorean triples where all three integers are less than or equal to 50. Do not list permutations of the same integers (e.g., do not list both (3, 4, 5) and (4, 3, 5) ). Use nested for loops.

This is what I have:

```#include <stdio.h>
main ()
{
int side1;
int side2;
int side3;

for (side1 = 1; side1 <= 50; side1++){

for (side2 = 1; side2 <= 50; side2++){

for (side3 = 1; side3 <= 50; side3++){

if ((side1 * side1) + (side2 * side2) == (side3 * side3)){
printf("%d, %d, %d\n", side1, side2, side3);
}

}
}
}

return 0;

}

```

Any help will be much appreciated, thanks.

Is This A Good Question/Topic? 0

## Replies To: nested for loops; while and if statements

### #2 apw5020

Reputation: 78
• Posts: 666
• Joined: 26-March 09

## Re: nested for loops; while and if statements

Posted 19 February 2011 - 09:06 PM

Here is one solution. Make an array called triples. When you find the first triple, the number you add to that array should be the sum of side1, side2, and side3. Then, when you find another triple, sum it and compare it against the triples already stored in your array. If it is already there, move on and if not then add it. Since 3+4+5=5+4+3 etc. it should skip over all duplicates.

This post has been edited by apw5020: 19 February 2011 - 09:07 PM

### #3 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

## Re: nested for loops; while and if statements

Posted 19 February 2011 - 09:09 PM

apw5020, on 19 February 2011 - 09:06 PM, said:

Here is one solution. Make an array called triples. When you find the first triple, the number you add to that array should be the sum of side1, side2, and side3. Then, when you find another triple, sum it and compare it against the triples already stored in your array. If it is already there, move on and if not then add it. Since 3+4+5=5+4+3 etc. it should skip over all duplicates.

is there a way to do it without arrays? i know its pretty complicated, but we aren't allowed to use stuff we haven't covered in class.

### #4 apw5020

Reputation: 78
• Posts: 666
• Joined: 26-March 09

## Re: nested for loops; while and if statements

Posted 19 February 2011 - 09:27 PM

Seems like a pain, but if you know how many unique triples there will be you could make that number of variables. Store in each of those variables the sum of the sides of the first instance of that triple. Then compare subsequent sums of triples to each of those variables. Do you see what I mean?

Does anyone have a better idea?

This post has been edited by apw5020: 19 February 2011 - 11:04 PM

### #5 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

## Re: nested for loops; while and if statements

Posted 19 February 2011 - 10:57 PM

apw5020, on 19 February 2011 - 09:27 PM, said:

Seems like a pain, but if you know how many unique triples there will be you could make that number of variables. Store in each of those variables the sum of the sides of the first instance of that triple. Then compare subsequent sums of triples to each of those variables. Do you see what I mean? Also, make sure you initialize them to zero so you don't try to compare a number to nothingness!

Does anyone have a better idea?

I see exactly what you mean, and you're right that would be a pain. Theres gotta be a better way of doing this

### #6 apw5020

Reputation: 78
• Posts: 666
• Joined: 26-March 09

## Re: nested for loops; while and if statements

Posted 19 February 2011 - 11:03 PM

for (side3 = 1; side3 <= side2; side3++)

### #7 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

## Re: nested for loops; while and if statements

Posted 19 February 2011 - 11:24 PM

apw5020, on 19 February 2011 - 11:03 PM, said:

for (side3 = 1; side3 <= side2; side3++)

I made the changes you suggested but for some reason I can't compile that particular program?!? i'm trying to remotely log into my school's linux server but it doesn't connect, maybe the storm outside has something to do with it. I'm not sure what to do at this point

### #8 CTphpnwb

• D.I.C Lover

Reputation: 3803
• Posts: 13,811
• Joined: 08-August 08

## Re: nested for loops; while and if statements

Posted 20 February 2011 - 06:58 AM

To avoid duplicates I would start side2 out at side1 + 1. That should be an easy change to your original code. Too easy to provide an example.

Oh, and main() needs to return an int. Fix that or you'll likely lose points on your grade.

This post has been edited by CTphpnwb: 20 February 2011 - 07:05 AM

### #9 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

## Re: nested for loops; while and if statements

Posted 20 February 2011 - 10:55 AM

CTphpnwb, on 20 February 2011 - 06:58 AM, said:

To avoid duplicates I would start side2 out at side1 + 1. That should be an easy change to your original code. Too easy to provide an example.

Oh, and main() needs to return an int. Fix that or you'll likely lose points on your grade.

do you mean increment/increase side2 as side2 = side1 + 1 in the second for loop? Or when I am declaring my variables? Or in the printf statement, the algorithm for getting side2? Sorry, I'm a newb to this stuff

### #10 CTphpnwb

• D.I.C Lover

Reputation: 3803
• Posts: 13,811
• Joined: 08-August 08

## Re: nested for loops; while and if statements

Posted 20 February 2011 - 11:08 AM

I mean that this line changes:
```        for (side2 = 1; side2 <= 50; side2++){
```

This should be more than enough for you to figure it out. Think about what this line does and what I said above.

### #11 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

## Re: nested for loops; while and if statements

Posted 22 February 2011 - 07:14 PM

CTphpnwb, on 20 February 2011 - 11:08 AM, said:

I mean that this line changes:
```        for (side2 = 1; side2 <= 50; side2++){
```

This should be more than enough for you to figure it out. Think about what this line does and what I said above.

eureka!

```#include <stdio.h>
int main ()
{
int side1;
int side2;
int side3;

for (side1 = 1; side1 <= 50; side1++){

for (side2 = side1; side2 <= 50; side2++){

for (side3 = side2; side3 <= 50; side3++){

if ((side1 * side1) + (side2 * side2) == (side3 * side3)){
printf("%d, %d, %d\n", side1, side2, side3);
}

}
}
}

return 0;

}

```

so i just initialized side2 = side1 and side3=side2, i can't believe i couldn't think of this before!! thanks guys