w3resource

C#: Check three given integers and return true if the difference between small and medium and the difference between medium and large is same


Check Equal Differences in Three Integers

Write a C# Sharp program to compare three integers (small, medium, and large) and return true if the difference between small and medium and the difference between medium and large is the same.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - 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.

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 three integers as arguments
            Console.WriteLine(test(4, 5, 6)); // Test with integers 4, 5, and 6
            Console.WriteLine(test(7, 12, 13)); // Test with integers 7, 12, and 13
            Console.WriteLine(test(-1, 0, 1)); // Test with integers -1, 0, and 1
            Console.ReadLine(); // Wait for user input before closing the console window
        }       

        // Function definition of test function that takes three integers 'x', 'y', and 'z' and returns a boolean value
        public static bool test(int x, int y, int z)
        {
            // Conditions to check the arithmetic sequence property between the three integers
            if (x > y && x > z && y > z) return x - y == y - z; // Check if x > y > z
            if (x > y && x > z && z > y) return x - z == z - y; // Check if x > z > y
            if (y > x && y > z && x > z) return y - x == x - z; // Check if y > x > z
            if (y > x && y > z && z > x) return y - z == z - x; // Check if y > z > x
            if (z > x && z > y && x > y) return z - x == x - y; // Check if z > x > y
            return z - y == y - x; // If none of the above conditions match, check if z > y > x
        }
    }
}

Sample Output:

True
False
True

Flowchart:

C# Sharp: Flowchart: 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.

For more Practice: Solve these Related Problems:

  • Write a C# program to return true if the sequence formed by three integers is arithmetic.
  • Write a C# program to check whether the absolute differences between adjacent numbers are equal.
  • Write a C# program to check if three integers can form a linear pattern with a common step.
  • Write a C# program to return true if all three values have a uniform increasing or decreasing difference.

Go to:


PREV : Nearest to 13 Without Crossing.
NEXT : Write a C# Sharp program to create a new  string using two given strings s1, s2, the format of the new string will be s1s2s2s1.

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.