Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class SequenceOfGivenSum
- {
- static void Main()
- {
- /*10.Write a program that finds in given array of integers a sequence of given sum S (if present).
- Example:{4, 3, 1, 4, 2, 5, 8}, S=11 {4, 2, 5}*/
- Console.Write("Array size: ");
- int arraySize = int.Parse(Console.ReadLine());
- int[] arr = new int[arraySize];
- int number;
- for (int index = 0; index < arraySize; index++)
- {
- Console.Write("Position {0} : ", index + 1);
- number = int.Parse(Console.ReadLine());
- arr[index] = number;
- }
- Console.Write("The sum your looking for: ");
- int sum = int.Parse(Console.ReadLine());
- int startPosition = 0, endPosition = 0, curentSum = 0;
- for (int i = 0; i < arr.Length-1; i++)
- {
- curentSum = arr[i];
- for (int k = i+1; k < arr.Length-1; k++)
- {
- curentSum += arr[k];
- if (curentSum == sum)
- {
- startPosition = i;
- endPosition = k;
- }
- if (curentSum > sum)
- {
- curentSum = 0;
- break;
- }
- }
- }
- for (int i = 3; i <= 5; i++)
- {
- Console.Write(arr[i] + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement