C#: Check three given integers and return true if one of them is 20 or more less than one of the others
C# Sharp Basic Algorithm: Exercise-51 with Solution
Check if One Integer 20 Less Than Another
Write a C# Sharp program to check three given integers and return true if one of them is 20 or more less than one of the others.
Visual Presentation:
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)
{
// Printing the results of the 'test' method with different integer values
Console.WriteLine(test(11, 21, 31)); // Output: True (since the differences between all combinations of numbers are greater than or equal to 20)
Console.WriteLine(test(11, 22, 31)); // Output: False (since the difference between 11 and 22 is less than 20)
Console.WriteLine(test(10, 20, 15)); // Output: True (since the differences between all combinations of numbers are greater than or equal to 20)
Console.ReadLine(); // Keeping the console window open
}
// Method to check if the absolute difference between any two numbers among x, y, and z is greater than or equal to 20
public static bool test(int x, int y, int z)
{
// Returns true if the absolute differences between x and y, x and z, or y and z are all greater than or equal to 20
return Math.Abs(x - y) >= 20 || Math.Abs(x - z) >= 20 || Math.Abs(y - z) >= 20;
}
}
}
Sample Output:
True True False
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to check if two or more non-negative given integers have the same rightmost digit.
Next: Write a C# Sharp program to find the larger from two given integers. However if the two integers have the same remainder when divided by 7,then the return the smaller integer. If the two integers are the same, return 0.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/basic-algo/csharp-basic-algorithm-exercises-51.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics