Advertisement
Teodor92

ZeroSum

Oct 29th, 2012
1,233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 KB | None | 0 0
  1. /* We are given 5 integer numbers. Write a program that
  2.  * checks if the sum of some subset of them is 0.
  3.  * Example: 3, -2, 1, 1, 8  1+1-2=0.
  4.  */
  5.  
  6. using System;
  7.  
  8. class SubsetSumZero
  9. {
  10.     static void Main()
  11.     {
  12.         Console.WriteLine("Enter the first number:");
  13.         int a = int.Parse(Console.ReadLine());
  14.         Console.WriteLine("Enter the second number:");
  15.         int b = int.Parse(Console.ReadLine());
  16.         Console.WriteLine("Enter the third number:");
  17.         int c = int.Parse(Console.ReadLine());
  18.         Console.WriteLine("Enter the fourth number:");
  19.         int d = int.Parse(Console.ReadLine());
  20.         Console.WriteLine("Enter the sixth number:");
  21.         int e = int.Parse(Console.ReadLine());
  22.         // 5 digits ===============================================================================
  23.         if (a + b + c + d + e == 0)
  24.         {
  25.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}|, |{3}|, |{4}|", a, b, c, d, e);
  26.         }
  27.         // 4 digits ===============================================================================
  28.         if (b + c + d + e == 0)
  29.         {
  30.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}|, |{3}| ", b, c, d, e);
  31.         }
  32.         if (a + c + d + e == 0)
  33.         {
  34.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}|, |{3}| ", a, c, d, e);
  35.         }
  36.         if (a + b + d + e == 0)
  37.         {
  38.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}|, |{3}| ", a, b, d, e);
  39.         }
  40.         if (a + b + c + e == 0)
  41.         {
  42.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}|, |{3}| ", a, b, c, e);
  43.         }
  44.         if (a + b + c + d == 0)
  45.         {
  46.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}|, |{3}| ", a, b, c, d);
  47.         }
  48.         // 3 digits ===============================================================================
  49.         if (c + d + e == 0)
  50.         {
  51.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}| ", c, d, e);
  52.         }
  53.         if (b + d + e == 0)
  54.         {
  55.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}| ", b, d, e);
  56.         }
  57.         if (a + d + e == 0)
  58.         {
  59.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}| ", a, d, e);
  60.         }
  61.         if (b + c + e == 0)
  62.         {
  63.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}| ", b, c, e);
  64.         }
  65.         if (a + c + e == 0)
  66.         {
  67.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}| ", a, c, e);
  68.         }
  69.         if (a + b + e == 0)
  70.         {
  71.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}| ", a, b, e);
  72.         }
  73.         if (b + c + d == 0)
  74.         {
  75.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}| ", b, c, d);
  76.         }
  77.         if (a + c + d == 0)
  78.         {
  79.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}| ", a, c, d);
  80.         }
  81.         if (a + b + d == 0)
  82.         {
  83.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}| ", a, b, d);
  84.         }
  85.         if (a + b + c == 0)
  86.         {
  87.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}|, |{2}| ", a, b, c);
  88.         }
  89.         // 2 digits ===============================================================================
  90.         if (d + e == 0)
  91.         {
  92.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}| ", d, e);
  93.         }
  94.         if (c + e == 0)
  95.         {
  96.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}| ", c, e);
  97.         }
  98.         if (b + e == 0)
  99.         {
  100.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}| ", b, e);
  101.         }
  102.         if (a + e == 0)
  103.         {
  104.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}| ", a, e);
  105.         }
  106.         if (c + d == 0)
  107.         {
  108.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}| ", c, d);
  109.         }
  110.         if (b + d == 0)
  111.         {
  112.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}| ", b, d);
  113.         }
  114.         if (a + d == 0)
  115.         {
  116.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}| ", a, d);
  117.         }
  118.         if (b + c == 0)
  119.         {
  120.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}| ", b, c);
  121.         }
  122.         if (a + c == 0)
  123.         {
  124.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}| ", a, c);
  125.         }
  126.         if (a + b == 0)
  127.         {
  128.             Console.WriteLine("The subset whos sum is zero is: |{0}|, |{1}| ", a, b);
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement