Advertisement
APXOHT

Untitled

Jan 6th, 2013
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1. using System;
  2.  
  3. class SequenceOfGivenSum
  4. {
  5.     static void Main()
  6.     {
  7.         int[] array = { 4, 3, 1, 4, 2, 5, 8 };
  8.         int S = int.Parse(Console.ReadLine());
  9.         int start = 0;
  10.         int sum = 0;
  11.         for (int i = 0; i < array.Length - 1; i++)
  12.         {
  13.             sum += array[i];
  14.             start = i;
  15.             for (int j = i + 1; j < array.Length; j++)
  16.             {
  17.                 sum += array[j];
  18.                 if (sum == S)
  19.                 {
  20.                     for (int k = start; k <= j; k++)
  21.                     {
  22.                         Console.Write(array[k] + " ");
  23.                     }
  24.                     return;
  25.                 }
  26.             }
  27.             sum = 0;
  28.         }
  29.         Console.WriteLine("The sum is not present in the array.");
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement