aspire12

7.EqualArrays

Feb 8th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _7.EqualArrays
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //Read two arrays and print on the console whether they are identical or not. Arrays are identical if their elements are equal. If the arrays are identical find the sum of the first one and print on the console following message: "Arrays are identical. Sum: {sum}", otherwise find the first index where the arrays differ and print on the console following message: "Arrays are not identical. Found difference at {index} index".
  10.  
  11.  
  12.             string[] array1 = Console.ReadLine().Split();
  13.             string[] array2 = Console.ReadLine().Split();
  14.             int sum = 0;
  15.             bool equal = true;
  16.             for (int i = 0; i < array1.Length; i++)
  17.             {
  18.                 if (array1[i] == array2[i])
  19.                 {
  20.                     sum += int.Parse(array1[i]);
  21.                 }
  22.                 else
  23.                 {
  24.                     equal = false;
  25.                     Console.WriteLine($"Arrays are not identical. Found difference at {i} index");
  26.                     break;
  27.                 }
  28.             }
  29.             if (equal)
  30.             {
  31.                 Console.WriteLine($"Arrays are identical. Sum: {sum}");
  32.             }
  33.         }
  34.     }
  35. }
Add Comment
Please, Sign In to add comment