Introduction :
The Pythagorean theorem is a basic theory in mathematics specially in geometry. It is mostly used in various fields like geometry, number theory, and computer science etc. A Pythagorean triple consists of three positive numbers (integers) which represent the lengths of the sides of a right angled triangle. It satisfies the Pythagorean theorem.
Suppose the side of a right angled triangle are a, b and c. If the side opposite the right angle (hypotenuse) is c then the Pythagorean formula (a2 + b2 = c2) must be fulfill.
Using the C programming language, you can generate and display Pythagorean triples on the console screen. Here, you have to use a nested loop to find and display Pythagorean triples. In this article, I shall show you how to find and display Pythagorean triples up to a certain limit in C programming language.
About the program :
This is a C program that finds and print a specified number of Pythagorean triples on the console screen. When you run the program, it asks you to enter how many Pythagorean triples you want to print. Then, it displays them on the screen.
Explanation of the program :
First, include stdio.h header file in the program. Then, declare integer variables (a, b, c, i and n) in the main() function. Now, ask the user to enter an integer number using printf() function. After that, get the input and store it in the “n” variable using scanf() function.
Using “if else” statement, check if the user has entered a number greater than zero (0) or not. If he enters wrong input, print an error message. Otherwise, calculate and print all the Pythagorean triples on the screen.
How run the program :
You have to install VS Code to run the program on your PC. Then open VS Code and create a new file like “pythagorean_triplets.c”. After that, copy the below code and paste in the “pythagorean_triplets.c” file. Now, save the file and run the program on your PC.
Source code of the program :
The following code is the source code of a program that finds and display Pythagorean triples in C programming language.
/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
int a, b, c, i = 0, n;
printf("Enter how many pythagorean triples you want to print\n");
scanf("%d", &n);
if (n <= 0)
{
printf("You have to enter a number greater than zero\n ");
}
else
{
printf("Your %d pythagorean triples number are\n", n);
for (c = 1; c <= 1000; c++)
{
for (a = 1; a <= 1000; a++)
{
for (b = 1; b <= 1000; b++)
{
if (c * c == (a * a + b * b) && a < b)
{
printf("%d + %d + %d = %d\n", a, b, c, a + b + c);
i++;
}
if (i == n)
break;
}
}
}
}
return 0;
}
Output of the program :
When you run the program, you can see the output on your PC.

Conclusion :
At last, you have learned how to find and display Pythagorean triples in the C programming language. Thank you for visiting my site.