【C語言】創建一個函數,判斷某一正整數是否為水仙花數,並調用這個函數找出1000以內所有水仙花數


#include <stdio.h>
int fun(int x)
{
    int a, b, c;
    a = x / 100;
    b = x % 100 / 10;
    c = x % 10;
    if (x == a * a * a + b * b * b + c * c * c)
        return 1;
    else
        return 0;
}
int main()
{
    int m;
    printf("1000以內的水仙花數:\n");
    for (m = 100; m < 1000; m++)
    {
        if(fun(m)==1)
        printf("%5d\n", m);
    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM