Skip to content

Instantly share code, notes, and snippets.

@gopigujjula
Created April 18, 2013 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gopigujjula/5412295 to your computer and use it in GitHub Desktop.
Save gopigujjula/5412295 to your computer and use it in GitHub Desktop.
Program to add two 50- digit numbers in C#
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment