Friday, February 26, 2016

Write a program that outputs all possibilities to put + or - or nothing between the numbers

Name of problem:
 
  1. Write a program that outputs all possibilities to put + or - or nothing between the numbers 1,2,…,9 (in this order) such that the result is 100. For example 1 + 2 + 3 - 4 + 5 + 6 + 78 + 9 = 100.

Solution:

  using System;

  namespace add_numers
   {
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, k, l, m, n;
            float sum;
            i = Convert.ToInt32(Console.ReadLine());
            j = Convert.ToInt32(Console.ReadLine());
            k = Convert.ToInt32(Console.ReadLine());
            l = Convert.ToInt32(Console.ReadLine());
            m = Convert.ToInt32(Console.ReadLine());
            n = Convert.ToInt32(Console.ReadLine());
            sum = i + j + k + l + m - n;
            Console.WriteLine(i + "+" + j + "+" + k + "+" + l + "+" + m + "-" + n + "="+ sum);
            Console.ReadKey();

        }
    }
}
 Result:  

No comments:

Post a Comment