+ 3

How to determine the largest smallest among the five (5) input numbers? In c#

18th Nov 2020, 8:05 AM
Joyce
Joyce - avatar
16 ответов
+ 5
Thankyouuuu
18th Nov 2020, 11:39 AM
Joyce
Joyce - avatar
+ 4
I tried but only 3 input numbers work, because when I tried the symbol "&&" up to 5 input numbers it's error huhu whats wrong
18th Nov 2020, 10:08 AM
Joyce
Joyce - avatar
+ 4
This what I meant in 5 input numbers but it's doesn't work:(( largest = (n1 > n2 && n1 > n3 && n1 > n4 && n1 > n5)? n1 : ((n2 > n1 && n3 > n2 && n4 > n3 && n5 > n4)? n2 : n3 : n4 : n5);
18th Nov 2020, 10:48 AM
Joyce
Joyce - avatar
+ 4
If you have to do it that way... largest = n1 > n2 && n1 > n3 && n1 > n4 && n1 > n5 ? n1 : (n2 > n3 && n2 > n4 && n2 > n5 ? n2 : (n3 > n4 && n3 > n5 ? n3 : (n4 > n5 ? n4 : n5)));
18th Nov 2020, 11:06 AM
ChaoticDawg
ChaoticDawg - avatar
18th Nov 2020, 1:25 PM
Priyanshi
Priyanshi - avatar
+ 3
What do you mean "the largest smallest"? are you searching for largest or smallest?
18th Nov 2020, 8:27 AM
Ipang
+ 3
I mean by by applying conditional operators
18th Nov 2020, 8:28 AM
Joyce
Joyce - avatar
+ 3
You can do this with a loop and conditionals. Declare 2 variables for <largest> and <smallest>, assign the first array element to the variables. Then run a loop through the array from the second to the fifth element. While running through the array, use `if` conditional to check whether element[ i ] is either larger than <largest>, or smaller than <smallest>. Do update value of <largest> or <smallest> depending on the conditional check result.
18th Nov 2020, 8:40 AM
Ipang
+ 3
I'm confused I'm just a beginner pls give me an example of methods
18th Nov 2020, 8:42 AM
Joyce
Joyce - avatar
+ 3
Give it a shot first, I'll get back to you later. Please post your tryout code link here so I can check it when I return. Or someone else may give you a code before then ...
18th Nov 2020, 8:49 AM
Ipang
+ 3
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int n1 = 10, n2 = 25, n3 = 32; int largest; largest = (n1 > n2 && n1 > n3)? n1 : ((n2 > n1 && n3 > n2)? n2 : n3); Console.WriteLine("Largest among {0} {1} {2} is {3}", n1, n2, n3, largest); Console.WriteLine("Press any key to exit"); Console.ReadKey(); } } }
18th Nov 2020, 10:08 AM
Joyce
Joyce - avatar
+ 2
There are also min/max methods that can be used if you don't absolutely have to use conditionals. Here's an example on the versions that can be used on enumerables, such as an array, not to be confused with Math.Min()/Math.Max(). https://code.sololearn.com/cSfLgQqz7bTr/?ref=app
18th Nov 2020, 8:58 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Joyce, Are you or are you not allowed to use a loop? I think the complexity of the ternary operation will increase as they are nested to deal with multiple numbers to evaluate. (Edit) As the nested ternary operation becomes more complex, the chances for logical fault may also increases.
18th Nov 2020, 10:17 AM
Ipang
+ 2
This will get the max of 5 using ternary operator. Flip all the comparison operators to get min. largest = n1 > n2 ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3); largest = largest > n4 ? (largest > n5 ? largest : n5) : (n4 > n5 ? n4 : n5); A loop as Ipang has stated will be much less complex and cleaner. It is recommended if the built-in min/max methods can't be used.
18th Nov 2020, 10:27 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg Looks like I had misunderstood what s/he meant about 5 input numbers. I took the liberty to assume the input was read into an array. In case of independent variables like this, I suppose use of loop is not an option anyways : )
18th Nov 2020, 10:37 AM
Ipang
+ 2
Ipang it is if you put them in an array first! As is the option to use the min/max methods. Lol
18th Nov 2020, 10:40 AM
ChaoticDawg
ChaoticDawg - avatar