Home > Enterprise >  C Language: Triple nested while() loop that does not work correctly
C Language: Triple nested while() loop that does not work correctly

Time:07-24

I want to write a code in C language to output a list of triples matching the Pythagorean triple in a range of positive integers (a2 = b2 c2 when a, b ,c ∈ ℤ ) using only nested while () loops.

I wrote this:

//Programme to output possible Pythagorean triples in a range of unsigned integers.

#include <stdio.h>

int main (void){

    unsigned int i = 1, j = 1, ij = 1;

    puts ("\n***To calculate Pythag. triple integers...***\n");
    while (i <= 250){
        while (j <= 250){
            while (ij <= 250){
                if (ij*ij == i*i   j*j){
                    printf ("Candidate triple: (%u, %u, %u)\n", ij, i, j);
                }
                  ij;
            }
              j;
        }
          i;
    }
    return 0;
}

It does not work. It only shows the text in the first puts () function.

output:

enter image description here

What's my mistake?

CodePudding user response:

After ending a loop with i=1,j=1,ij=251, you never enter the innermost loop again because you never set ij back to 1. Same for j.

    unsigned i = 1;
    while (i <= 250){
        unsigned j = 1;
        while (j <= 250){
            unsigned ij = 1;
            while (ij <= 250){
                if (ij*ij == i*i   j*j){
                    printf ("Candidate triple: (%u, %u, %u)\n", ij, i, j);
                }
                  ij;
            }
              j;
        }
          i;
    }

We usually write this as follows:

    for ( unsigned i = 1; i <= 250;   i ) {
        for ( unsigned j = 1; j <= 250;   j ) {
            for ( unsigned ij = 1; ij <= 250;   ij ) {
                if (ij*ij == i*i   j*j){
                    printf ("Candidate triple: (%u, %u, %u)\n", ij, i, j);
                }
            }
        }
    }

This is still a while loop, but using the for keyword/syntax.

CodePudding user response:

After inner loop exit it's variable isn't reinitialized and remain 251. You should initialize variable inside of outer loop:

   while (i <= 250){
        j = 1;
        while (j <= 250){
            ij = 1;
            while (ij <= 250){
                if (ij*ij == i*i   j*j){
                    printf ("Candidate triple: (%u, %u, %u)\n", ij, i, j);
                }
                  ij;
            }
              j;
        }
          i;
    }
  • Related