C# Sharp Basic Algorithm Exercises: Check which number is nearest to the value 100 among two given integers
C# Sharp Basic Algorithm: Exercise-19 with Solution
Write a C# Sharp program to check which number is nearest to the value 100 among two given integers. Return 0 if the two numbers are equal.
Pictorial Presentation 1:
Pictorial Presentation 2:
Sample Solution:
C# Sharp Code:
using System;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(test(78, 95));
Console.WriteLine(test(95, 95));
Console.WriteLine(test(99, 70));
Console.ReadLine();
}
public static int test(int x, int y)
{
const int n = 100;
var val = Math.Abs(x - n);
var val2 = Math.Abs(y - n);
return val == val2 ? 0 : (val < val2 ? x : y);
}
}
}
Sample Output:
95 0 99
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to check the largest number among three given integers.
Next: Write a C# Sharp program to check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework