w3resource

C#: Accept two integers and return true if either one is 5 or their sum or difference is 5


Check for 5 or Sum/Difference Equals 5

Write a C# Sharp program that accepts two integers and returns true if either is 5 or their sum or difference is 5.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Accept two integers and return true if either one is 5 or their sum or difference is 5.

Sample Solution:-

C# Sharp Code:

using System;
using System.Linq;

namespace exercises
{
    // Class declaration
    class Program
    {
        // Main method - entry point of the program
        static void Main(string[] args)
        {
            // Calling the 'test' method with different integers and printing the results
            Console.WriteLine(test(5, 4));  // Output: True
            Console.WriteLine(test(4, 3));  // Output: False
            Console.WriteLine(test(1, 4));  // Output: True
            Console.ReadLine(); // Keeping the console window open
        }

        // Method to check different conditions involving two integers
        public static bool test(int x, int y)
        {
            // Check if any of the following conditions are true:
            // 1. 'x' is equal to 5
            // 2. 'y' is equal to 5
            // 3. The sum of 'x' and 'y' is equal to 5
            // 4. The absolute difference between 'x' and 'y' is equal to 5
            return x == 5 || y == 5 || x + y == 5 || Math.Abs(x - y) == 5;
        }
    }
}

Sample Output:

True
False
True

Flowchart:

C# Sharp: Flowchart: Accept two integers and return true if either one is 5 or their sum or difference is 5.

For more Practice: Solve these Related Problems:

  • Write a C# program to return true if either number is 7 or their product or absolute difference is 7.
  • Write a C# program to return true if one number is a multiple of 5 or their average is 5.
  • Write a C# program to return true if either number is 3 or their sum or bitwise XOR equals 3.
  • Write a C# program to return true if both numbers are within 5 of each other or either is 5.

Go to:


PREV : Sum in Range 10-20 Returns 30.
NEXT : Multiple of 13 or 1 More.

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.