Advertisement
Guest User

04 Grab and Go - Arrays and Methods

a guest
Jun 13th, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _04_Grab_and_Go
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] arrayNumbers = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  11.             int n = int.Parse(Console.ReadLine());
  12.             long sum = 0;
  13.  
  14.             for (int pos = arrayNumbers.Length - 1; pos >= 0; pos--)
  15.             {
  16.                 if (arrayNumbers[pos] == n)
  17.                 {
  18.                     for (int i = 0; i < pos; i++)
  19.                     {
  20.                         sum += arrayNumbers[i];
  21.                     }
  22.                     break;
  23.                 }
  24.             }
  25.  
  26.             if (sum == 0)
  27.             {
  28.                 Console.WriteLine("No occurrences were found!");
  29.             }
  30.             else
  31.             {
  32.                 Console.WriteLine(sum);
  33.             }
  34.            
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement