Question

In: Computer Science

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). in C prgramming

Solutions

Expert Solution

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!!!.


Related Solutions

The number 73 is written as a sum of three natural numbers 73=a+b+c (the triple (a,b,c)...
The number 73 is written as a sum of three natural numbers 73=a+b+c (the triple (a,b,c) is ordered; e.g., the decompositions 73=19+20+34 and 73=20+34+19 are different. Also, assume that all the decompositions have equal probability.) Given that there exists a triangle with sides a, b, and c, what is the probability that this triangle is isosceles?
Prove or disprove: If a, b, c are any three distinct positive integers such that 1/a...
Prove or disprove: If a, b, c are any three distinct positive integers such that 1/a + 1/b + 1/c = 1, then a + b + c is a prime
Prove or disprove: If a, b, c are any three distinct positive integers such that 1/a...
Prove or disprove: If a, b, c are any three distinct positive integers such that 1/a + 1/b + 1/c = 1, then a + b + c is a prime.
A Pythagorean triplet is a set of positive integers (x, y, z) such that x2 +...
A Pythagorean triplet is a set of positive integers (x, y, z) such that x2 + y2 = z2. Write an interactive script that asks the user for three positive integers (x, y, z, in that order). If the three numbers form a Pythagorean triplet the script should a) display the message ‘The three numbers x, y, z, form a Pythagorean triplet’ b) plot the corresponding triangle using red lines connecting the triangle edges. Hint: place the x value on...
Find two positive integers such that the sum of the first number and four times the...
Find two positive integers such that the sum of the first number and four times the second number is 100 and the product of the numbers is as large as possible. please double check answer
Show that if a, b are positive integers and d = hcf(a, b), then there are...
Show that if a, b are positive integers and d = hcf(a, b), then there are positive integers s, t such that d = sa − tb.
Let S{a, b, c, d} be a set of four positive integers. If pairs of distinct...
Let S{a, b, c, d} be a set of four positive integers. If pairs of distinct elements of S are added, the following six sums are obtained:5,10, 11,13,14,19. Determine the values of a, b, c, and d. (There are two possibilities. )
Input 10 integers and display the following: a. the sum of even numbers. b. the sum...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum of odd numbers. c. the largest integer d. the smallest integer e. the total count of even numbers f. the total count of odd numbers. Using C++ program with for loop..
If (x,y,z) is a primitive Pythagorean triple, prove that z= 4k+1
If (x,y,z) is a primitive Pythagorean triple, prove that z= 4k+1
How many eight-digit positive integers have the sum of digits being even?
How many eight-digit positive integers have the sum of digits being even?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT