-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
44 lines (41 loc) · 1.37 KB
/
Copy pathProgram.cs
File metadata and controls
44 lines (41 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class MethodOverloading
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Please enter a number to calculate its square:");
int number = Convert.ToInt32(Console.ReadLine());
int squareResult = Calculate(number);
Console.WriteLine($"The square of {number} is {squareResult}.");
Console.WriteLine("Please enter two numbers to calculate their sum:");
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());
int sumResult = Calculate(num1, num2);
Console.WriteLine($"The sum of {num1} and {num2} is {sumResult}.");
}
catch (FormatException)
{
Console.WriteLine("Invalid input. Please enter a valid number.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
finally
{
Console.WriteLine("Thank you for using the method overloading example.");
}
}
static int Calculate(int num)
{
return num * num;
}
static int Calculate(int a, int b)
{
return a + b;
}
}
}