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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,771 questions

55,537 answers

573 users

How to find the first Pythagorean triple in C

1 Answer

0 votes
#include <stdio.h>
#include <math.h>

/*
A Pythagorean triple is a set of three positive integers (a, b, c)
that satisfy the Pythagorean theorem:

        a^2 + b^2 = c^2

These represent the side lengths of a right triangle.
The smallest and most famous Pythagorean triple is (3, 4, 5).
*/

/* Function to find the first Pythagorean triple */
void findFirstPythagoreanTriple(void) {
    for (int a = 1; a <= 100; a++) {
        for (int b = a; b <= 100; b++) {
            int c2 = a * a + b * b;   /* c^2 */
            int c = (int)sqrt((double)c2);

            if (c * c == c2) {        /* Check if c is an integer */
                printf("First Pythagorean triple: %d, %d, %d\n", a, b, c);
                return;
            }
        }
    }
}

int main(void) {
    findFirstPythagoreanTriple();
    
    return 0;
}



/*
run:

First Pythagorean triple: 3, 4, 5

*/

 



answered Jun 19 by avibootz
...