p4

Write a C program that will take 3 positive integers as input and verify whether they form a Pythagorean triplet or not.

Pythagorean triplet consists of three positive integers X, Y, and Z, such that 
X2 + Y2 = Z2. A right angled triangle whose sides are Pythagorean triplet is called a Pythagorean triangle. For example : 3, 4 and 5 are pythagorean triplet(32 + 42 = 52).


/*Program to take 3 positive integers as input and verify whether they
from a pythagorean triplet or not */

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

void main()
{

int a,b,c,gre,num,num1,num2,sum,sqr;
clrscr();

printf("\nEnter three +ve numbers:");

printf("\nEnter first number:");
scanf("%d",&a);
printf("\nEnter second number:");
scanf("%d",&b);
printf("\nEnter third number:");
scanf("%d",&c);

num=a*a;
num1=b*b;
num2=c*c;


if(a>b && a>c)
{
sqr=sqrt(num1+num2);
gre=a;

}
else
if(b>c)
{
sqr=sqrt(num2+num);
gre=b;
}
else
{
sqr=sqrt(num+num1);
gre=c;
}

if(sqr==gre)
{
printf("\nPythagorean Triangle possible ");
}
else
{
printf("\nTriangle not possible");
}

getch();
}

No comments:

Post a Comment