In: Computer Science
Three positive integers (a, b, c) with a<b<c are called a Pythagorean triple if the sum of the square of a and the square of b is equal to the square of c. Write a program that prints all Pythagorean triples (one in a line) with a, b, and c all smaller than 1000, as well the total number of such triples in the end. Arrays are not allowed to appear in your code. Hint: user nested loops (Can you make your code as efficient as possible? But this is not required). in C prgramming
Hello,Thanks for asking to answer.I hope you are safe doing well in this pandemic.In this i am providing you the solution.If there any problem in the code.Do let me know in the comments.
Code:-
#include<stdio.h>
void main(){
int n;
int a,b,c=0;
int m=2;
int i=0;
int count =0;
printf("Enter the range:-");
scanf("%d",&n);
while (c < n) {
// now loop on j from 1 to i-1
for ( i = 1; i < m; ++i) {
//using the formula to find the triplets
// a, b and c
a = m * m - i * i;
b = 2 * m * i;
c = m * m + i * i;
if (c > n)
break;
count++;
printf("%d %d %d\n",a,b,c);
}
m++;
}
printf("Total pyhthagorian pairs
between %d is = %d",n,count);
}
Output:- You can change the limit according to your need.
I hope the above code fulfiled Your needs.However,if there is any problem in the code or it needs modification do let me know in the comments.And if this solution is helpful please provide a Upvote to this answer.
Thanks.Happy Learning!!!.