Question

Please use only C language and NOT C++ or Java You are now allowed to use...

Please use only C language and NOT C++ or Java
You are now allowed to use the following • for loops • math.h and pow() function • formatting float and double numbers • switch statements • getchar() function • ASCII chart • do…while loops • break and continue statements • Logical AND (&&), logical OR (||), logical NOT (!) operators

Pythagorean triples are three positive integer numbers a, b, c that form the sides of a right triangle, such that a^2 + b^2 = c^2 . Use a brute-force method using triple nested for loops to generate all Pythagorean triples where sides a, b, and c are less than or equal to 500. Do not worry about duplicate sets of {a, b, c } such as { 3, 4, 5} and { 5, 4, 3}. Brute force techniques are not the most elegant solutions but can be used to solve interesting problems for which there is no known algorithm to solve the problem.

Sample Output:

Set 1 - Pythagorean Triple: {3, 4, 5}

Set 2 - Pythagorean Triple: {4, 3, 5}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C Program:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//Main function
int main()
{
    int a, b, c, set=0;

    //Outer loop
    for(a=1; a<=500; a++)
    {
        //Inner loop 1
        for(b=1; b<=500; b++)
        {
            //Inner loop 2
            for(c=1; c<=500; c++)
            {
                //Checking pythagorean triples
                if( (pow(a, 2) + pow(b, 2)) == (pow(c,2)) )
                {
                    //Incrementing set
                    set++;

                    //Printing sets
                    printf("\n Set %d - Pythagorean Triple: {%d, %d, %d} ", set, a, b, c);
                }
            }
        }
    }

    return 0;
}

___________________________________________________________________________________________

Sample Run:

回 X CNTC1Pythagorean bin\DebugiPythagorean.exe Set 737Pythagorean Triple: 408, 170, 442 Set 738 Pythagorean Triple £416, 87,

Add a comment
Know the answer?
Add Answer to:
Please use only C language and NOT C++ or Java You are now allowed to use...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • to be done in c language. follow prompt Second Program: triples.c 8. Create a program named...

    to be done in c language. follow prompt Second Program: triples.c 8. Create a program named triples.c "C How to Program", 8th edition, problem 4.27 on page 154. In other editions, it may be on a different page Here's the problem... A right triangle can have sides that are all integers. The set of 3 integers for the sides of a right triangle is called a Pythagorean triple Write a C program to find all of the triples with hypotenuse...

  • Write the java program: A right triangle can have sides whose lengths are all integers. The...

    Write the java program: A right triangle can have sides whose lengths are all integers. The set of three integer values for the length of the sides of a triangle is called a Pythagorean triple. The length of the three sides must satisfy the relationship that the sum of the squares of the sides is equal to the square of the hypotenuse. Write a Java application that prompts the user for an integer that represents the largest side value and...

  • Re doing. A Pythagorean triple is a set of integers (a, b, c) satisfying a2+bc2. Write pseudocode...

    re doing. A Pythagorean triple is a set of integers (a, b, c) satisfying a2+bc2. Write pseudocode (or Matlab code) that will output the number of unique Pythagorean triples that satisfy c< 200. [Unique means that you should not count (a 3, b 4, c 5) and (a 4, b 3, c 5) as two different triples, for example]. Explain why your program will work. Imagine a square n x n matrix A with diagonal elements which we believe to...

  • please prove the theorems, thank you very much 8.21 Theorem. A natural numbern can be written...

    please prove the theorems, thank you very much 8.21 Theorem. A natural numbern can be written as a sum of two squares of natural numbers if and only if every prime congruent to 3 modulo 4 in the unique prime factorization of n occurs to an even power Pythagorean triples revisited We are now in a position to describe the possible values for the hypotenuse in a primitive Pythagorean triple. 8.22 Theorem. If (a, h, e) is a primitive Pythagorean...

  • C code. Write a program to find all the triangles with integer side lengths and a...

    C code. Write a program to find all the triangles with integer side lengths and a user specified perimeter. Perimeter is the sum of all the sides of the triangle. Integers a, b and c form a triangle if the sum of the lengths of any two sides is greater than the third side. Your program should find all the triples a, b and c where a + b + c is user specified perimeter value. Print each triple only...

  • Please solve this C language progrm (2-D Array program) the most basic way ! Thank You!...

    Please solve this C language progrm (2-D Array program) the most basic way ! Thank You! Write a function that takes a 4 x 4 array of integers as its parameter. The function must use two nested for-loops to determine the largest integer in the array as well as its row and column numbers. Recall that C-functions cannot return multiple values. Use pointers in the function parameter list to relay the results to the caller program. Write another function, which...

  • Please respond to as many of the following prompts as you can by writing readable and...

    Please respond to as many of the following prompts as you can by writing readable and valid arguments that exhibit mathematical fluency to the extent that you can do this. 1. Recall that we discussed number systems by writing an ordered triple (X, Y, Z), where X is a set of things we call `numbers', Y is the notation for an operation we call `addition', and Z is a notation for what we call `multiplication'. We can do something analogous...

  • need help with these 3 homework problems please !! Use Inverse Functions to Express Solutions to...

    need help with these 3 homework problems please !! Use Inverse Functions to Express Solutions to Trigonometric Equations 13. Solve on the interval [0, 21). 4 cos' x-7 cos x + 3 = 0 Solve a Right Triangle Pythagorean theorem: d'+b = c? 4+B+C =180° sin e OPP hyp cos adi, tan = opp hyp adj b hyp opp sce byp - cote adj OPP adj 1. Solve the right triangle. Round the lengths of sides to 1 decimal place....

  • Please respond to as many of the following prompts as you can by writing readable and...

    Please respond to as many of the following prompts as you can by writing readable and valid arguments that exhibit mathematical fluency to the extent that you can do this. please show work clearly and show which answer is for what question. You do not need to answer the second question, I just have it to clear what the third question is about 1. Recall that we discussed number systems by writing an ordered triple (X, Y, Z), where X...

  • C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include ...

    C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include <stdio.h> int main() { int fries; // A flag denoting whether they want fries or not. char bacon; // A character for storing their bacon preference. double cost = 0.0; // The total cost of their meal, initialized to start at 0.0 int choice; // A variable new to version 2, choice is an int that will store the // user's menu choice. It will also serve as our loop control...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT