Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace _08.SumOfArraysInteger
- {
- class SumOfArraysInteger
- {
- static int[] ArrayInput()
- {
- Console.WriteLine("Enter the length of the array: ");
- int n = int.Parse(Console.ReadLine());
- int[] array = new int[n];
- //Reverse
- for (int i = n - 1; i >= 0; i--)
- {
- Console.Write("Enter the digit: ");
- array[i] = int.Parse(Console.ReadLine());
- }
- return array;
- }
- static void PrintFormated(int[] array)
- {
- for (int i = array.Length - 1; i >= 0; i--)
- {
- Console.Write(array[i]);
- }
- }
- static List<int> SumOfIntegerArrays(int[] arrayX, int[] arrayY)
- {
- List<int> sumList = new List<int>();
- int currentSum = 0;
- byte special = 0;
- int loopLengthMax = Math.Max(arrayX.Length, arrayY.Length);
- int loopLengthMin = Math.Min(arrayX.Length, arrayY.Length);
- for (int i = 0; i < loopLengthMax; i++)
- {
- if (i < loopLengthMin)
- {
- currentSum = (arrayX[i] + arrayY[i] + special);
- if (currentSum >= 10)
- {
- currentSum = currentSum % 10;
- special = 1;
- }
- else
- {
- special = 0;
- }
- sumList.Add(currentSum);
- }
- if (i >= loopLengthMin)
- {
- if (arrayX.Length > arrayY.Length)
- {
- currentSum = arrayX[i] + special;
- if (currentSum >= 10)
- {
- currentSum = currentSum % 10;
- special = 1;
- }
- else
- {
- special = 0;
- }
- sumList.Add(currentSum);
- }
- if (arrayX.Length < arrayY.Length)
- {
- currentSum = arrayY[i] + special;
- if (currentSum >= 10)
- {
- currentSum = currentSum % 10;
- special = 1;
- }
- else
- {
- special = 0;
- }
- sumList.Add(currentSum);
- }
- }
- }
- if (special > 0) sumList.Add(special);
- sumList.Reverse();
- return sumList;
- }
- static void Main()
- {
- int[] firstArray = ArrayInput();
- int[] secondArray = ArrayInput();
- PrintFormated(firstArray);
- Console.Write(" + ");
- PrintFormated(secondArray);
- Console.Write(" = ");
- foreach (var item in SumOfIntegerArrays(firstArray, secondArray))
- {
- Console.Write(item);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement