[Programmers] 하샤드 수 - 연습문제

동민·2021년 3월 10일
// 하샤드 수 - 연습문제
public class Harshad {
	public boolean solution(int x) {

		String s = Integer.toString(x);
		int sum = 0;

		for (int i = 0; i < s.length(); i++) {
			sum += Integer.parseInt(s.charAt(i) + "");
		}
		return x % sum == 0 ? true : false;
	}

	public static void main(String[] args) {

		Harshad s = new Harshad();

		System.out.println(s.solution(10));
		System.out.println(s.solution(12));
		System.out.println(s.solution(11));
		System.out.println(s.solution(13));
		System.out.println(s.solution(5));

	}
}
profile
BE Developer

0개의 댓글