Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Saturday, November 8, 2014

Program to take 3 positive integers as input and verify whether they from a pythagorean triplet or not in C.



Code Box
---------------------------------------------

/*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();
}


---------------------------------------------


Code Box 2
---------------------------------------------

/*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);


if(a==0 || b==0 ||c==0)
{
printf("\nside zero not possible ");
goto next;
}


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

printf("squares of numbers are:");
printf("\nsquare of a = %d",num);
printf("\nsquare of b = %d",num1);
printf("\nsquare of c = %d",num2);


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

printf("\nGeater is a");
}
else
if(b>c)
{
sqr=sqrt(num2+num);
gre=b;
printf("\nGeater is b");

}
else
{
sqr=sqrt(num+num1);
gre=c;

printf("\nGeater is c");
}

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

next:
getch();
}

No comments: