Advertisement
striking

SubsetEqualZero

Nov 14th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.70 KB | None | 0 0
  1. // We are given 5 integer numbers. Write a program that checks if the sum of some subset of them is 0. Example: 3, -2, 1, 1, 8  1+1-2=0.
  2.  
  3.  
  4. using System;
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         Console.WriteLine("Write 5 integer numbers:\n");
  10.         Console.Write("First one: ");
  11.         int first = int.Parse(Console.ReadLine());
  12.  
  13.         Console.Write("Second one: ");
  14.         int second = int.Parse(Console.ReadLine());
  15.  
  16.         Console.Write("Third one: ");
  17.         int third = int.Parse(Console.ReadLine());
  18.  
  19.         Console.Write("Fourth one: ");
  20.         int fourth = int.Parse(Console.ReadLine());
  21.  
  22.         Console.Write("Fifth one: ");
  23.         int fifth = int.Parse(Console.ReadLine());
  24.         bool check = false;
  25.  
  26.         // We have 26 options between 5 numbers
  27.  
  28.         if (first + second == 0)
  29.         {
  30.             Console.WriteLine("The sum of this subset {0} {1} is: 0", first, second);
  31.             check = true;
  32.         }
  33.  
  34.         if (first + third == 0)
  35.         {
  36.             Console.WriteLine("The sum of this subset {0} {1} is: 0", first, third);
  37.             check = true;
  38.         }
  39.  
  40.         if (first + fourth == 0)
  41.         {
  42.             Console.WriteLine("The sum of this subset {0} {1} is: 0", first, fourth);
  43.             check = true;
  44.         }
  45.  
  46.         if (first + fifth == 0)
  47.         {
  48.             Console.WriteLine("The sum of this subset {0} {1} is: 0", first, fifth);
  49.             check = true;
  50.         }
  51.  
  52.         if (second + third == 0)
  53.         {
  54.             Console.WriteLine("The sum of this subset {0} {1} is: 0", second, third);
  55.             check = true;
  56.         }
  57.  
  58.         if (second + fourth == 0)
  59.         {
  60.             Console.WriteLine("The sum of this subset {0} {1} is: 0", second, fourth);
  61.             check = true;
  62.         }
  63.  
  64.         if (second + fifth == 0)
  65.         {
  66.             Console.WriteLine("The sum of this subset {0} {1} is: 0", second, fifth);
  67.             check = true;
  68.         }
  69.  
  70.         if (third + fourth == 0)
  71.         {
  72.             Console.WriteLine("The sum of this subset {0} {1} is: 0", third, fourth);
  73.             check = true;
  74.         }
  75.  
  76.         if (third + fifth == 0)
  77.         {
  78.             Console.WriteLine("The sum of this subset {0} {1} is: 0", third, fifth);
  79.             check = true;
  80.         }
  81.  
  82.         if (fourth + fifth == 0)
  83.         {
  84.             Console.WriteLine("The sum of this subset {0} {1} is: 0", fourth, fifth);
  85.             check = true;
  86.         }
  87.  
  88.         if (first + second + third == 0)
  89.         {
  90.             Console.WriteLine("The sum of this subset {0} {1} {2} is: 0", first, second, third);
  91.             check = true;
  92.         }
  93.  
  94.         if (second + third + fourth == 0)
  95.         {
  96.             Console.WriteLine("The sum of this subset {0} {1} {2} is: 0", second, third, fourth);
  97.             check = true;
  98.         }
  99.  
  100.         if (third + fourth + fifth == 0)
  101.         {
  102.             Console.WriteLine("The sum of this subset {0} {1} {2} is: 0", third, fourth, fifth);
  103.             check = true;
  104.         }
  105.  
  106.         if (first + second + fourth == 0)
  107.         {
  108.             Console.WriteLine("The sum of this subset {0} {1} {2} is: 0", first, second, fourth);
  109.             check = true;
  110.         }
  111.  
  112.         if (first + second + fifth == 0)
  113.         {
  114.             Console.WriteLine("The sum of this subset {0} {1} {2} is: 0", first, second, fifth);
  115.             check = true;
  116.         }
  117.  
  118.         if (second + third + fifth == 0)
  119.         {
  120.             Console.WriteLine("The sum of this subset {0} {1} {2} is: 0", second, third, fifth);
  121.             check = true;
  122.         }
  123.  
  124.         if (first + fourth + fifth == 0)
  125.         {
  126.             Console.WriteLine("The sum of this subset {0} {1} {2} is: 0", first, fourth, fifth);
  127.             check = true;
  128.         }
  129.  
  130.         if (second + fourth + fifth == 0)
  131.         {
  132.             Console.WriteLine("The sum of this subset {0} {1} {2} is: 0", second, fourth, fifth);
  133.             check = true;
  134.         }
  135.  
  136.         if (first + third + fourth == 0)
  137.         {
  138.             Console.WriteLine("The sum of this subset {0} {1} {2} is: 0", first, second, fourth);
  139.             check = true;
  140.         }
  141.  
  142.         if (third + fourth + first == 0)
  143.         {
  144.             Console.WriteLine("The sum of this subset {0} {1} {2} is: 0", third, fourth, first);
  145.             check = true;
  146.         }
  147.  
  148.         if (first + second + third + fourth == 0)
  149.         {
  150.             Console.WriteLine("The sum of this subset {0} {1} {2} {3} is: 0", first, second, third);
  151.             check = true;
  152.         }
  153.  
  154.         if (second + third + fourth + fifth == 0)
  155.         {
  156.             Console.WriteLine("The sum of this subset {0} {1} {2} {3} is: 0", second, third, fourth);
  157.             check = true;
  158.         }
  159.  
  160.         if (first + second + fourth + fifth == 0)
  161.         {
  162.             Console.WriteLine("The sum of this subset {0} {1} {2} {3} is: 0", first, second, fourth);
  163.             check = true;
  164.         }
  165.  
  166.         if (first + second + third + fifth == 0)
  167.         {
  168.             Console.WriteLine("The sum of this subset {0} {1} {2} {3} is: 0", first, second, third, fifth);
  169.             check = true;
  170.         }
  171.  
  172.         if (first + third + fourth + fifth == 0)
  173.         {
  174.             Console.WriteLine("The sum of this subset {0} {1} {2} {3} is: 0", first, third, fourth, fifth);
  175.             check = true;
  176.         }
  177.  
  178.         if (first + second + third + fourth + fifth == 0)
  179.         {
  180.             Console.WriteLine("The sum of this subset {0} {1} {2} {3} {4} is: 0", first, second, third, fourth, fifth);
  181.             check = true;
  182.         }
  183.  
  184.         if (check == false)
  185.         {
  186.             Console.WriteLine("There is no subset Equal 0!");
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement