모든 일상

C# 프로그래머스 Lv1 하샤드 수 본문

코딩 공부/C#

C# 프로그래머스 Lv1 하샤드 수

통통푸린 2021. 6. 16. 08:45
728x90
반응형

public class Solution
{
    public bool solution(int x) 
    {
        bool answer;
        string arr = x.ToString();
        int[] num = new int[arr.Length];
        int result = 0;
        int add = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            num[i] = int.Parse(arr.Substring(i, 1));
            add += num[i];
        }
        result = x % add;
        if(result == 0)
            answer = true;
        else
            answer = false;
        return answer;
    }
}

728x90
반응형