Latest web development tutorials

C Exercise Example 3

100 cases of classic C language 100 cases of classic C language

Title: An integer that after 100 plus is a perfect square, plus 168 is a perfect square, what this number is how much?

Program analysis: less than 100,000 judgment, plus the number of the first 100 and then prescribing, and then add the number to 268 before prescribing, if the result of evolution after the following conditions are met, that is the result.

Source Code:

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

int main() {
    long int i,x,y;
    for (i=1;i<100000;i++) {
        x=sqrt(i+100); // x为加上100后开方后的结果
        y=sqrt(i+268); // y为再加上168后开方后的结果
        if(x*x==i+100 && y*y==i+268){ //如果一个数的平方根的平方等于该数,这说明此数是完全平方数
            printf("\n%ld\n",i);
        }
    }
}

The above example output is:


21

261

1581

100 cases of classic C language 100 cases of classic C language