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:  

make a frame in c#

Problem:

Write a function that takes a list of strings an prints them, one per line, in a rectangular frame. For example the list ["Hello", "World", "in", "a", "frame"] gets printed as:
*********
* Hello *
* World *
* in    *
* a     *
* frame *
*********





Solution:

using System.Text;
using System.Threading.Tasks;

namespace helloWorldInAFrame
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("*********************");
            Console.WriteLine("*" + "         " + "Hallo " +"   " +"**");
            Console.WriteLine("*" + "       " + "world " + "    " + "***");
            Console.WriteLine("*" + "             " + "in " + " " + "***");
            Console.WriteLine("*" + "         " + "A Frame " + " " + "**");
            Console.WriteLine("*********************");
            Console.ReadKey();
        }
    }
}


Result:

leap year calculation

using System;
namespace leapyear
{
    class Program
    {
        static void Main(string[] args)
        {
            int yr;
            yr = Convert.ToInt32(Console.ReadLine());
            if ((yr % 4 != 0) || (yr % 100 == 0 && yr % 400 != 0))
            {
                Console.WriteLine("this is not a leap yr");

            }
            else if ((yr % 4 == 0) && (yr % 100 != 0))
            {
                Console.WriteLine("this is leap year");

            }
            else
                Console.WriteLine("this is leap year");            
       
            Console.ReadKey();
        }
    }
}