140. 快樂的數字

 avatar
user_6817964
c_cpp
2 years ago
540 B
3
Indexable
#include <stdbool.h>
#include <stdio.h> 


bool isHappy(int);


bool isHappy(int n) {
    while (n != 1) {
        int sum = 0;

        while (n > 0)
        {
            int d = n % 10;
            n /= 10;
            sum += d * d;
        }

        if (sum == 4) {
            return false;
        }
        n = sum;
    }
    return true;
}

int main() {
    int n;
    scanf("%d", &n);
    if (isHappy(n)) {
        printf("true");
    }
    else {
        printf("false");
    }
    return 0;
}
Editor is loading...