SECTION
Published 2019. 10. 26. 23:43
[Codewar] Square Every Digit PS/Codewar

https://www.codewars.com/kata/square-every-digit/train/java

 

Codewars: Train your coding skills

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

 

Instructions

 

Welcome. In this kata, you are asked to square every digit of a number.

For example, if we run 9119 through the function, 811181 will come out, because 92 is 81 and 12 is 1.

 

Note: The function accepts an integer and returns an integer

 

 

Solution

import java.util.stream.Stream;

public class SquareDigit {

  public int squareDigits(int n) {
      int[] digits = Stream.of(String.valueOf(n)
                    .split(""))
                    .mapToInt(Integer::parseInt)
                    .toArray();
                    
      String sum ="";
      for(int i=0; i<digits.length; i++)
      {
        sum += Integer.toString(digits[i] * digits[i]);  
      }
      int sum1 = Integer.parseInt(sum);
      return sum1;
  }
}
profile

SECTION

@SectionR0

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그