For every natural number m>1; 2m, m-1 and m +1 form a Pythagorean triplet. Write a program to input the value of 'm' through console to display a Pythagorean Triplet Sample Input: 3 Then 2m=6, m--1=8 and m2+1=10 Thus 6, 8, 10 form a Pythagorean Triplet'.
Answers
import java.util.Scanner;
public class KboatPythagoreanTriplet
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of m: ");
int m = in.nextInt();
if (m < 2) {
System.out.println("Invalid Input, m should be greater than 1");
return;
}
int firstTriplet = 2 * m;
int secondTriplet = (int)(Math.pow(m, 2) - 1);
int thirdTriplet = (int)(Math.pow(m, 2) + 1);
System.out.println("Pythagorean Triplets: "
+ firstTriplet
+ ", "
+ secondTriplet
+ ", "
+ thirdTriplet);
}
}