“Persegi setiap digit” Kode Jawaban

Jika kita menjalankan 9119 melalui fungsi, 811181 akan keluar, karena 92 ​​adalah 81 dan 12 adalah 1.

function squareDigits(num){
    return Number(('' + num).split('').map(function (val) { return val * val;}).join(''));
}
Attractive Albatross

C# kuadrat setiap digit angka

//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()));
        }
Wide-eyed Wolf

Persegi setiap digit


function squareDigits(num){
    
    //first change the number to string 

  let strnum = String(num)
  let result = ''

  //Do search of the String (number)

  for(let i =0; i<strnum.length;i++){
      //For math we need change it again to int 
     let squared = parseInt(strnum[i])*parseInt(strnum[i])
     //and after that change again to string 
     result = result+ String(squared)
      
  }
  // The final change to int 

  return parseInt(result)
  
  }

  console.log(squareDigits(9119))
ABS

Jawaban yang mirip dengan “Persegi setiap digit”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya