Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         var arr = Console.ReadLine().Split().Select(int.Parse).ToArray();
  8.         var num = int.Parse(Console.ReadLine());
  9.  
  10.         int arrLenght = arr.Length;
  11.         int lastIndex = 0;
  12.         int sum = 0;
  13.         bool noNumber = true;
  14.  
  15.         for (int i = 0; i < arrLenght; i++)
  16.         {
  17.             if (arr[i] == num)
  18.             {
  19.                 noNumber = false;
  20.                 lastIndex = i;
  21.             }
  22.         }
  23.         if (noNumber == false)
  24.         {
  25.             for (int i = 0; i < lastIndex; i++)
  26.             {
  27.                 sum += arr[i];
  28.             }
  29.             Console.WriteLine(sum);
  30.         }
  31.         else if (noNumber == true)
  32.         {
  33.             Console.WriteLine("No occurrences were found!");
  34.         }
  35.         else if(lastIndex == 0)
  36.         {
  37.             Console.WriteLine(sum);
  38.         }
  39.  
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement