The following code will print upto 20 the triangle whos hypotenuse is a complete square. You can extend the limits by doing some changes in the code to your desire.
#include <stdio.h>
#include <math.h> // for sqrt root function
int main ()
{
int b,p; // initialization
int h; // initialization
printf("All the Applicable Triangles are:\n");
printf("Base \t Perp \t Hyp \n"); // to represent sides of triangle which are satisfied by pythagoras theorem
for(b=1;b<=19;b++) // first loop starts with one side from 1-19
{ for (p=1; p<=19;p++) // second loop starts with b=loop state untill p=19
{ for (h=sqrt((p*p)+(b*b));h==sqrt((p*p)+(b*b));h++) // 3rd loop takes the first input only and becomes false for nxt loop so no further execution occurs
if (h>=20) // if hyp is more than 20 sides wont be printed
break;
else
{printf(" %d \t %d \t %d \n",b,p,h); // sides printed if h^2=b^2+p^2 ans is integer
} // end of third loop
} // end of second loop
} //end of first loop
return 0;
} //end main
Have a Read!
Hi Everyone.
The idea for this page is to provide new c users simple c tutorials to learn how to develop the logic and more importantly correct way to write syntax.
ENJOY !!!
Thursday, January 27, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment