Last active
June 30, 2022 13:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example run A
Example run B