Skip to content

Instantly share code, notes, and snippets.

@Yousif-FJ
Last active June 30, 2022 13:53
Show Gist options
  • Save Yousif-FJ/4232a1ff91a86e3decebce3e8196a37e to your computer and use it in GitHub Desktop.
Save Yousif-FJ/4232a1ff91a86e3decebce3e8196a37e to your computer and use it in GitHub Desktop.
A- Write a C# CLI program to read three numbers and find the maximum ( largest ) number. B- Modify the program to read ten numbers and find the second largest number.
//Answer to A
using System;
using System.Linq;
namespace ConsoleApp2_for_test
{
class Program
{
public static void Main()
{
Console.WriteLine("Input 3 numbers to find the largest");
var num1 = Console.ReadLine();
var num2 = Console.ReadLine();
var num3 = Console.ReadLine();
var largestNumber = FindLargestNumber(int.Parse(num1), int.Parse(num2),int.Parse(num3));
Console.WriteLine($"The largest number is {largestNumber} ");
}
public static int FindLargestNumber(params int[] numbers)
{
return numbers.Max();
}
}
}
//Answer to B
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp2_for_test
{
class Program
{
private const int NumberOfNumberToInput = 10;
public static void Main()
{
Console.WriteLine($"Input {NumberOfNumberToInput} numbers to find the second largest");
var numberArray = new List<int>();
for (int i = 0; i < NumberOfNumberToInput; i++)
{
var input = Console.ReadLine();
numberArray.Add(int.Parse(input));
}
var secondLargetsNumber = FindSecondLargestNumber(numberArray.ToArray());
Console.WriteLine($"The second largest number is {secondLargetsNumber} ");
}
public static int FindLargestNumber(params int[] numbers)
{
return numbers.Max();
}
public static int FindSecondLargestNumber(params int[] numbers)
{
var orderedList = numbers.OrderByDescending(n => n).ToArray();
return orderedList[1];
}
}
}
@Yousif-FJ
Copy link
Author

Example run A
image

Example run B
image

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