C語言精髓-完美數

#include
#include <math.h>
int IsPerfect(int x);
int main()
{
    int m;
    printf("Input m:");
    scanf("%d", &m);
     
    if (IsPerfect(m))  /* 完全數判定 */
        printf("%d is a perfect number\n", m);
    else
        printf("%d is not a perfect number\n", m);
 
    return 0;
}
 
/* 函數功能:判斷完全數,若函數返回0,則代表不是完全數,若返回1,則代表是完全數 */
int IsPerfect(int x)
{
    int i;
    int total = 0;          /* 1沒有真因子,不是完全數 */
 
    for (i=1;i<x;i++)
    {
        if (x%i == 0)
            total = total + i;
    }
 
    return total==x ? 1 : 0;     
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章