C# Sharp Exercises: Get the absolute value of the difference between two given numbers
C# Sharp Basic: Exercise-20 with Solution
Write a C# program to get the absolute value of the difference between two given numbers. Return double the absolute value of the difference if the first number is greater than second number.
Sample Solution:-
C# Sharp Code:
using System;
using System.Collections.Generic;
public class Exercise20 {
static void Main(string[] args)
{
Console.WriteLine(result(13, 40));
Console.WriteLine(result(50, 21));
Console.WriteLine(result(0, 23));
}
public static int result(int a, int b)
{
if (a > b)
{
return (a - b)*2;
}
return b - a;
}
}
Sample Output:
27 58 23
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# program to compute the sum of two given integers, if two values are equal then return the triple of their sum.
Next: Write a C# program to check the sum of the two given integers and return true if one of the integer is 20 or if their sum is 20.
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