Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

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()));
        }
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# check valid datetime 
Csharp :: after each vue router 
Csharp :: c# memorystream to byte array 
Csharp :: using tmp unity 
Csharp :: how to set progress openedge driver name for odbc connection c# 
Csharp :: remove backcolor c# 
Csharp :: unity how to rotate something to point to something else 
Csharp :: oncollisionenter 
Csharp :: new parameterized thread c# 
Csharp :: c# unity 2d play video 
Csharp :: c#image to bytes 
Csharp :: c# close 1 form open another form 
Csharp :: c# get files of type in directory 
Csharp :: unity iterate all child objects 
Csharp :: remove all letters from string c# 
Csharp :: how to configure session timeout in asp.net core 
Csharp :: cshtml foreach 
Csharp :: How to add a label programatically in c# 
Csharp :: c# sql duplicate key exception 
Csharp :: js invoke async function blazor 
Csharp :: c# get month number from name 
Csharp :: unity round to x decimals 
Csharp :: ession in class c# 
Csharp :: get text component unity 
Csharp :: c# func with no return 
Csharp :: enable fullscreen unity code 
Csharp :: c# restart app 
Csharp :: how to move towards an object unity 
Csharp :: trigger collider unity 
Csharp :: c# datagridview column size 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =