w3resource

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


Nearest to 13 Without Crossing

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.

For more Practice: Solve these Related Problems:

  • Write a C# program to return the closest number to 13 from a list of three, without exceeding 13.
  • Write a C# program to return the maximum number less than or equal to 13 from two given integers.
  • Write a C# program to check which number is closer to 13 but not equal, and return the difference.
  • Write a C# program to return the closer number to 13 unless both are greater, then return -1.

Go to:


PREV : Sum Ignoring 10-20 Except 13, 17.
NEXT : Check Equal Differences in Three Integers.

C# Sharp Code Editor:



Improve this sample solution and post your code through Disqus

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.