Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

What's The REAL Secret To First Date Success With a Woman? Click Here To Find Out

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

29,372 questions

38,322 answers

573 users

How to find all pythagorean triples (a^2 + b^2 = c^2) from an array in C

Freaking Awesome WordPress Hosting
24 views
asked Sep 20, 2022 by avibootz
edited Sep 20, 2022 by avibootz

1 Answer

0 votes
#include <stdio.h>

void PrintPythagoreanTriples(int arr[], int size) {
    for (int i = 0; i < size - 2; i++) {
        for (int j = i + 1; j < size - 1; j++) {
            for (int k = i + 2; k < size; k++) {
                int a = arr[i];
                int b = arr[j];
                int c = arr[k];
                if (a * a + b * b == c * c) {
                    printf("%d %d %d\n", a, b, c);
                }
            }
        }
    }
}

int main()
{
    int arr[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int size = sizeof(arr) / sizeof(arr[0]);

    PrintPythagoreanTriples(arr, size);

    return 0;
}




/*
run:

3 4 5
6 8 10

*/

 


Protect Your Privacy - Download VPN


answered Sep 20, 2022 by avibootz
edited Sep 21, 2022 by avibootz
...