Answers for "kata, you are asked to square every digit of a number and concatenate them"

0

c# square every digit of a number

//c#  square every digit of a number 
public static int SquareDigits(int n)
        {
            var val = n.ToString();

            var result = "";

            for (var i = 0; i < val.Length; i++)
            {
                char c = val[i];
                int num = int.Parse(c.ToString());
                result += (num * num);
            }
            return int.Parse(result);
        }
  ------------------------------------------------Option 2
  public static int SquareDigits(int n)
        {
            var result =
         n
        .ToString()
        .ToCharArray()
        .Select(Char.GetNumericValue)
        .Select(a => (a * a).ToString())
        .Aggregate("", (acc, s) => acc + s);
            return int.Parse(result);
        }
-------------------------------------------------option 3
public static int SquareDigits(int n)
        {
            List<int> list = new List<int>();
            while (n != 0)
            {
                int remainder = n % 10;
                n = n / 10;
                list.Add((remainder * remainder));
            }
            return int.Parse(String.Join("", list.ToArray()));
        }
Posted by: Guest on July-08-2020
0

square every digit of a number ruby

number = 12345
number.to_s.chars.map { |num| num.to_i ** 2 }.join.to_i
# convert number to string (.to_s), convert string to array (.chars)
# .map each array element to the result of a block call. 
# .join array into string & convert back to integer.
Posted by: Guest on November-26-2020

Code answers related to "kata, you are asked to square every digit of a number and concatenate them"

Code answers related to "Javascript"

Browse Popular Code Answers by Language