Skip to content

Instantly share code, notes, and snippets.

@zuriby
Created December 12, 2025 10:04
Show Gist options
  • Select an option

  • Save zuriby/602b4e7159e8cb4eda3aa5e06412cb14 to your computer and use it in GitHub Desktop.

Select an option

Save zuriby/602b4e7159e8cb4eda3aa5e06412cb14 to your computer and use it in GitHub Desktop.
Console.WriteLine("Enter two integers (separated by comma or space):");
string[] parts = Console.ReadLine().Split(new char[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2 && int.TryParse(parts[0], out int a) && int.TryParse(parts[1], out int b))
{
Console.WriteLine(Math.Max(a, b));
}
else
{
Console.WriteLine("Invalid input. Please enter exactly two integers separated by comma or space.");
}
```
**What's happening:**
- `Split(new char[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries)` — handles both delimiters and ignores extra spaces
- `int.TryParse(..., out int a)` — attempts to parse; returns `false` if it fails (no exceptions)
- `Math.Max(a, b)` — returns the larger of the two
**Sample runs:**
```
Enter two integers (separated by comma or space):
37, 73
73
Enter two integers (separated by comma or space):
5 3
5
Enter two integers (separated by comma or space):
hello world
Invalid input. Please enter exactly two integers separated by comma or space.
@zuriby
Copy link
Author

zuriby commented Dec 12, 2025

Console.WriteLine("Enter two integers (separated by comma or space):");
string[] parts = Console.ReadLine().Split(new char[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);

if (parts.Length == 2 && int.TryParse(parts[0], out int a) && int.TryParse(parts[1], out int b))
{
Console.WriteLine(Math.Max(a, b));
}
else
{
Console.WriteLine("Invalid input. Please enter exactly two integers separated by comma or space.");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment