Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Pythagorean Triples in C

Pythagorean Triples in C

 

A Pythagorean triple consists of three positive integers a, b, and c, such that a² + b² = c². Such a triple is commonly written, and a well-known example is. If is a Pythagorean triple, then so is for any positive integer k. A primitive Pythagorean triple is one in which a, b and c are co-prime.
In this page we will look into a program in C programming language to find Pythagorean Triples from an array

pythgorean triplets

Problem Statement

Write a program in C programming language where you need to find the pythagorean triples from this list of array

Input Format
    -First line will contain the number of elements present in the array.
    -Second line will contain the elements of array

Output Format
   
-Output contains the space separated integers which are pythgorean tripes.

Sample Test Case : 1

Input
6
3 4 6 5 7 8

Output
3 4 5

Sample Test Case : 2

Input
5
10 4 6 12 5

Output
no triples found

Pythagorean Triples in C

Program for finding pythgorean triples in C

#include <stdio.h> 
 
int isTriplet(int ar[]int n

    int xyz;
    for (int i = 0i < ni++) 
    { 
        for (int j = i + 1j < nj++) 
        { 
            for (int k = j + 1k < nk++) 
            { 
                // Calculate square of array elements 
                x = ar[i] * ar[i], y = ar[j] * ar[j], z = ar[k] * ar[k]; 
                if (x == y + z || y == x + z || z == x + y)
                {
                    printf("%d, %d, %d",ar[i],ar[j],ar[k]);
                    return 1
                }   
            } 
        } 
    } 
  
    // If we reach here, no triplet found 
    return -1

  
/* Driver program to test above function */
int main() 

    int n;
    scanf("%d",&n);
    int ar[n];
    for(int i=0i<ni++)
    {
        scanf("%d",&ar[i]);
    }
    int result = isTriplet(arn);
    if(result == -1)
    {
        printf("no triples found");
    } 
    return 0

Learn About Arrays in C

Click on the below button to learn about in C Programming Language

Learn Arrays in C

Learn Data Structures

Click on the below button to study more Data Structure

Learn Data Structures