loading...

re: Daily Coding Problem #1 VIEW POST

FULL DISCUSSION
 

using System;

namespace PlayGround
{
class DailyProblems
{
int[] numArray;
int arraySize = 0;
int sumValue = 0;

    public void ElementsSum()
    {
        Console.Write("Enter sum value: ");
        sumValue = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the size of the array :");
        arraySize = Convert.ToInt32(Console.ReadLine());
        // Initialize array
        numArray = new int[arraySize];

        // Get input for the array elements
        for (int i = 0; i < arraySize; i++)
        {
            Console.Write("Element " + (i + 1) + ": ");
            numArray[i] = Convert.ToInt32(Console.ReadLine());
        }

        Console.WriteLine("Moment of truth...: " + MyEvaluate());
        Console.ReadLine();
    }

    private bool MyEvaluate()
    {
        bool flag = false;
        int difValue = 0;

        for (int i = 0; i < arraySize; i++)
        {
            if (sumValue > numArray[i])
            {
                difValue = sumValue - numArray[i];
                for (int k = i+1; k < arraySize; k++)
                {
                    if (difValue == numArray[k])
                        return true;
                }
            }
        }

        return flag;
    }
}

}

code of conduct - report abuse