Exercise[6B(e),Let Us C Solution

 Exercise[6B(e)]

Write a  program to generate all Pythagorean Triplets with side length
 less than or equal to 30.    */
 
 #include<stdio.h>
 #include<math.h>
 int main()
 {
 	int a,b;
 	float c;
 	// calculate the another side using Pythagorean Theorem
 	// a*a+b*b=c*c
 	// c=sqrt(a*a+b*b)
 	for(a=1;a<=30;a++)
 	{
 		for(b=1;b<=30;b++)
 		{
 			c=sqrt(a*a+b*b);
 			if(c==(int)c)
 			{
 				printf("(%d %d %d)\n",a,b,(int)c);
			 }
		 }
	 }
 	return 0;
 }

Post a Comment

0 Comments