Note :This post is first published on Apr-2013 in my previous blog Techkindle. Moving the content here.
Below code, without using BigInteger structure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace BigNumberAddition | |
{ | |
class Addition | |
{ | |
static void Main(string[] args) | |
{ | |
int[] firstNumber = new int[50] { | |
1, 2, 3, 4, 5, 6, 7, 8, 9, | |
1, 2, 3, 4, 5, 6, 7, 8, 9, | |
1, 2, 3, 4, 5, 6, 7, 8, 9, | |
1, 2, 3, 4, 5, 6, 7, 8, 9, | |
1, 2, 3, 4, 5, 6, 7, 8, 9, | |
1, 2, 3, 4, 5 | |
}; | |
int[] secondNumber = new int[50] { | |
1, 2, 3, 4, 5, 6, 7, 8, 9, | |
1, 2, 3, 4, 5, 6, 7, 8, 9, | |
1, 2, 3, 4, 5, 6, 7, 8, 9, | |
1, 2, 3, 4, 5, 6, 7, 8, 9, | |
1, 2, 3, 4, 5, 6, 7, 8, 9, | |
1, 2, 3, 4, 5 | |
}; | |
int[] resultNumber = new int[50]; | |
int temp = 0; | |
for (int i = firstNumber.Length – 1; i >= 0; i–) | |
{ | |
resultNumber[i] = firstNumber[i] + secondNumber[i] + temp; | |
if (resultNumber[i] > 9 && i != 0) | |
{ | |
temp = Convert.ToInt32(resultNumber[i].ToString().Substring(0, resultNumber[i].ToString().Length – 1)); | |
resultNumber[i] = Convert.ToInt32(resultNumber[i].ToString().Remove(0, resultNumber[i].ToString().Length – 1)); | |
} | |
else | |
temp = 0; | |
} | |
Console.WriteLine("First " + firstNumber.Length + " digits number: "); | |
foreach (int i in firstNumber) | |
Console.Write(i); | |
Console.WriteLine("\n\nSecond " + secondNumber.Length + " digits number: "); | |
foreach (int i in secondNumber) | |
Console.Write(i); | |
Console.WriteLine("\n\nResultant number after addition: "); | |
foreach (int i in resultNumber) | |
Console.Write(i); | |
Console.ReadKey(); | |
} | |
} | |
} |
Output
Happy Learning 🙂