if we run 9119 through the function, 811181 will come out, because 92 is 81 and 12 is 1.
function squareDigits(num){
return Number(('' + num).split('').map(function (val) { return val * val;}).join(''));
}
if we run 9119 through the function, 811181 will come out, because 92 is 81 and 12 is 1.
function squareDigits(num){
return Number(('' + num).split('').map(function (val) { return val * val;}).join(''));
}
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()));
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us