Write a program that will find pythagorean triplets between 1 and 100.
Answers
Answered by
1
Answer:
Pythagorean triplets are sides of right angled triangle . We can find it by using this formula:
2m, m sq.-1, m sq.+1
For example - 6, 8, 10
12, 16,20
24, 32, 40 etc.
Answered by
17
Required program :-
class Pythagorean
{
void main ( )
{
int a , b , c ;
for (a = 1 ; a <= 100 ; a++)
{
for (b = a + 1 ; b <= 100 ; b++ )
{
for (c = b + 1 ; c <= 100 ; c++ )
{
if (a*a + b*b == c*c);
System.out.println (a + "\t" + b + "\t" + c);
}
}
}
}
}
Explaination :-
- Line 3 : Three numbers are entered. a , b , c. Value of a is 1 , b is 2 , c is 3.
- Line 6 : For loop is used. Loop has been started from 1 and runned till 100. With an increment of 1.
- Line 8 : Loop has been entered for b.
- Line 10 : Loop has been entered for c.
- Line 12 : If condition has been used. If the given condition is true then the message is displayed.
- Line 13 : Message is displayed.
★ Working of loop :-
Similar questions