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). Write in C programming
#include <stdio.h> int main() { int count = 0; for (int a = 1; a < 1000; ++a) { for (int b = a+1; b < 1000; ++b) { for (int c = b+1; c < 1000; ++c) { if (a*a + b*b == c*c) { printf("%d %d %d\n", a, b, c); ++count; } } } } printf("Number of Pythagorean triples = %d\n", count); return 0; }
Get Answers For Free
Most questions answered within 1 hours.