How to convert an array of multi‑digit numbers to a number in C#

1 Answer

0 votes
using System;
 
class Program
{
    // ------------------------------------------------------------
    // ArrayToNumber
    // Converts an int[] into a single integer by concatenating
    // each element as a string. Works for multi-digit numbers.
    // Example: {14, 6, 9, 31, 20} ->14693120
    // ------------------------------------------------------------
    static int ArrayToNumber(int[] arr) {
        string s = "";
 
        foreach (int num in arr) {
            s += num.ToString();   // concatenate as text
        }
 
        return int.Parse(s);        // convert final string to int
    }
 
    static void Main()
    {
        int[] arr = { 14, 6, 9, 31, 20 };
 
        int n = ArrayToNumber(arr);
 
        Console.WriteLine("n = " + n);
    }
}
 
 
 
/*
run:
 
n = 14693120
 
*/
 

 



answered May 10 by avibootz
...