Question

Three positive integers (a, b, c) with a<b<c are called a Pythagorean triple if the sum...

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

Homework Answers

Answer #1
#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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Three positive integers (a, b, c) with a<b<c are called a Pythagorean triple if the sum...
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...
Write a C program, called logic_abc.c, that prompts the user to enter three integers a, b,...
Write a C program, called logic_abc.c, that prompts the user to enter three integers a, b, c, and then computes the results of the following logical operations, in sequence: !a || !b++ && c (a-1 || b/2) && (c*=2) (a-- || --b) && (c+=2) a || !(b && --c)
Write a program called gcd that accepts two positive integers in the range of [32,256] as...
Write a program called gcd that accepts two positive integers in the range of [32,256] as parameters and prints the greatest common divisor (GCD) of the two numbers. The GCD of two integers a and b is the largest integer that is a factor of both a and b. One efficient way to compute the GCD of two numbers is to use Euclid’s algorithm, which states the following: GCD (a, b) _ GCD (b, a % b) GCD (a, 0)...
Class work # 12 / Chapter 6 –> Arrays 3 (two dimensional arrays) Part 1: Create...
Class work # 12 / Chapter 6 –> Arrays 3 (two dimensional arrays) Part 1: Create a 2-by-3 integer array and initialize it with 6 integers of your choice. Using nested for loops, display the contents of the array on the screen Part 2: (Continue on part 1) Display the sum of all the integers in the array that you created in part 1. Create a function Part 3: Create a function that will display the elements of the array...
Using nested loops, write a function called primes(a,b) that takes in two positive integers a and...
Using nested loops, write a function called primes(a,b) that takes in two positive integers a and b (where a<b). Then simply RETURNS a string containing all the prime numbers between a and b (or if there are none, the string "No Primes"). You should check that a and b are valid inputs, that is, that a and b are integers such that a<b (otherwise, the function should print “No Primes”). Three sample calls of your function (in IDLE) should produce...
Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code...
Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code without testing it. I will downvote your answer and mark as spam.** I have posted this question several times, do not copy / paste the same code that has been posted, IT DOESN'T WORK!! In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
A. Write C code to create a structure called time_of_day, which stores the current time in...
A. Write C code to create a structure called time_of_day, which stores the current time in hours, minutes, and seconds. All the fields should be integers except for seconds, which should be a floating point value. B. Write a C function called check_time, which takes a pointer to a time_of_day structure as input, and return 1 if the time is valid (0 to 23 hours, 0 to 59 minutes, 0 to 59.999999 seconds) and 0 otherwise. Assume that times are...
Write some C++ program segments that solves the indicated tasks (you do not have to write...
Write some C++ program segments that solves the indicated tasks (you do not have to write a complete program, nor be concerned about "good" output; a small code segment will be enough). a) A program that gets a double number from the user, decides whether that number is positive, negative, or zero and display its decision on the screen. a) A function isPositive that takes as input a double number and returns the integer 1 if the number is positive,...
Problem: Our Armstrong number Please write code for C language So far we have worked on...
Problem: Our Armstrong number Please write code for C language So far we have worked on obtaining individual digits from 4 digits or 5 digit numbers. Then added them to find the sum of digits in various examples and assignments. However, the process of extracting individual digits is actually can be solved using a loop as you were doing a repetitive task by using mod operation and division operation. Now, we know how loops work and we can remove the...