0

Code note giving desired output

I made this c code for printing Pythagorean triples with Max hypo 500:- #include<stdio.h> int main() { int a,b,c,m=2,n; while(c<=500) { for(n=1;n<m;n++) { a=2*m*n; b=m*m-n*n; c=m*m+n*n; printf("%d,%d,%d\n",a,b,c); } m++; } return 0; } But it is giving values of c>500 till 545 as well. why?

20th Aug 2017, 1:13 AM
Shantanu Shinde
Shantanu Shinde - avatar
1 Respuesta
0
You're only checking the value of c in your while loop, but you change and output its value in the nested for loop where there is no control over the size of c. Try adding an if statement in the nested for loop that checks for the size of c and then break from the for loop prior to output if the desired max is exceeded. Something like: c=m*m+n*n; if(c>=500) { break; }
20th Aug 2017, 6:29 AM
ChaoticDawg
ChaoticDawg - avatar