Advertisement
sylviapsh

Is Some Sum Of 5 Int Nums Equal To Zero

Dec 28th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using System;
  2. class IsSomeSumOf5IntNumsEqualToZero
  3. {
  4.   static void Main()
  5.   {
  6.     //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.
  7.  
  8.     Console.Write("Enter the first integer number: ");
  9.     int num1 = int.Parse(Console.ReadLine());
  10.     Console.Write("Enter the second integer number: ");
  11.     int num2 = int.Parse(Console.ReadLine());
  12.     Console.Write("Enter the third integer number: ");
  13.     int num3 = int.Parse(Console.ReadLine());
  14.     Console.Write("Enter the fourth integer number: ");
  15.     int num4 = int.Parse(Console.ReadLine());
  16.     Console.Write("Enter the fifth integer number: ");
  17.     int num5 = int.Parse(Console.ReadLine());
  18.  
  19.     int sum = 0;
  20.  
  21.     for (byte combination0 = 0; combination0 < 2; combination0++)
  22.     {
  23.       for (byte combination1 = 0; combination1 < 2; combination1++)
  24.       {
  25.         for (byte combination2 = 0; combination2 < 2; combination2++)
  26.         {
  27.           for (byte combination3 = 0; combination3 < 2; combination3++)
  28.           {
  29.  
  30.             for (byte combination4 = 0; combination4 < 2; combination4++)
  31.             {
  32.               sum = 0;
  33.               if ((combination0 != 0) || (combination1 != 0) || (combination2 != 0) || (combination3 != 0) || (combination4 != 0))
  34.               {
  35.                 sum = combination0 * num1 + combination1 * num2 + combination2 * num3 + combination3 * num4 + combination4 * num5;
  36.  
  37.                 if (sum == 0)
  38.                 {
  39.                   Console.WriteLine("There is at least one subset Sum = 0!");
  40.                   return;
  41.                 }
  42.               }
  43.             }
  44.           }
  45.         }
  46.       }
  47.     }
  48.   }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement