w3resource

C#: Check two given integers and return the value whichever value is nearest to 13 without going over


C# Sharp Basic Algorithm: Exercise-58 with Solution

Write a C# Sharp program to check two integers and return the value nearest to 13 without crossing over. Return 0 if both numbers exceed.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Check two given integers and return the value whichever value is nearest to 13 without going over.
C# Sharp: Basic Algorithm Exercises - Check two given integers and return the value whichever value is nearest to 13 without going over.

Sample Solution:-

C# Sharp Code:

using System; // Import the System namespace which contains fundamental types and base classes
using System.Collections.Generic; // Import the System.Collections.Generic namespace for collections
using System.Linq; // Import the LINQ namespace for LINQ functionality
using System.Text; // Import the System.Text namespace for string manipulation
using System.Threading.Tasks; // Import the System.Threading.Tasks namespace for asynchronous operations

namespace exercises // Define a namespace called 'exercises'
{
    class Program // Define a class named 'Program'
    {
        static void Main(string[] args) // The entry point of the program
        {
            // Output the result of test function with different pairs of integers as arguments
            Console.WriteLine(test(4, 5)); // Test with integers 4 and 5
            Console.WriteLine(test(7, 12)); // Test with integers 7 and 12
            Console.WriteLine(test(10, 13)); // Test with integers 10 and 13
            Console.WriteLine(test(17, 33)); // Test with integers 17 and 33
            Console.ReadLine(); // Wait for user input before closing the console window
        }

        // Function definition of test function that takes two integers 'x' and 'y' and returns an integer
        public static int test(int x, int y)
        {
            // Conditional checks based on the values of 'x' and 'y' to determine the returned result
            if (x > 13 && y > 13) // If both 'x' and 'y' are greater than 13, return 0
                return 0;
            if (x <= 13 && y > 13) // If 'x' is less than or equal to 13 and 'y' is greater than 13, return 'x'
                return x;
            if (y <= 13 && x > 13) // If 'y' is less than or equal to 13 and 'x' is greater than 13, return 'y'
                return y;
            return x > y ? x : y; // If none of the above conditions are met, return the greater of 'x' and 'y'
        }
    }
}

Sample Output:

5
12
13
0

Flowchart:

C# Sharp: Flowchart: Check two given integers and return the value whichever value is nearest to 13 without going over.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to compute the sum of the three given integers. However, if any of the values is in the range 10..20 inclusive then that value counts as 0, except 13 and 17.
Next: Write a C# Sharp program to check three given integers (small, medium and large) and return true if the difference between small and medium and the difference between medium and large is same.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.