Write the following code in c #,Write a method called CaculateDifference(int firstNumber, int secondNumber). This method will calculate and return the difference between the two numbers i.e. it will subtract the smaller one from the larger one and then return that value. Call the CaculateDifference() method four times from your program Main() method supplying different arguments each time. You must display the returned value for each call
时间: 2024-02-13 12:05:00 浏览: 83
基于C#开发的找两个数组的不同值
5星 · 资源好评率100%
Here's the code in C#:
```csharp
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(CalculateDifference(10, 5)); // Output: 5
Console.WriteLine(CalculateDifference(8, 12)); // Output: 4
Console.WriteLine(CalculateDifference(20, 10)); // Output: 10
Console.WriteLine(CalculateDifference(-5, 3)); // Output: 8
}
static int CalculateDifference(int firstNumber, int secondNumber)
{
int difference;
if (firstNumber > secondNumber)
{
difference = firstNumber - secondNumber;
}
else
{
difference = secondNumber - firstNumber;
}
return difference;
}
}
}
```
In this code, we define a method called `CalculateDifference` that takes two `int` parameters `firstNumber` and `secondNumber`. The method calculates the difference between the two numbers by subtracting the smaller one from the larger one and returns the result.
In the `Main` method, we call `CalculateDifference` four times with different arguments and display the returned value for each call using `Console.WriteLine`.
阅读全文