Get the Largest among the four numbers.
-
Hi all, i am new to programming in C#. i dont know to code it to get the output ie., the largest among the four numbers and the user will give the input as numbers. and the munbers are int type inta,b,c,d; i need the code for largest among the four numbers of int a,b,c,d. pleae help me in sending code. regards, chitra.
-
Hi all, i am new to programming in C#. i dont know to code it to get the output ie., the largest among the four numbers and the user will give the input as numbers. and the munbers are int type inta,b,c,d; i need the code for largest among the four numbers of int a,b,c,d. pleae help me in sending code. regards, chitra.
you can apply a simple algorithm, if (a > b) { if (a > C) { if (a > d) { return a; } else { return d; } } else { if (c > d) { return c; } else { return d; } } } else { if (b > C) { if (b > d) { return b; } else { return d; } } else { if (c > d) { return c; } else { return d; } } }
Learning is a never ending process of Life.
-
Hi all, i am new to programming in C#. i dont know to code it to get the output ie., the largest among the four numbers and the user will give the input as numbers. and the munbers are int type inta,b,c,d; i need the code for largest among the four numbers of int a,b,c,d. pleae help me in sending code. regards, chitra.
There's a simpler algorithm:
int Max4(int a, int b, int c, int d) { return Math.Max(Math.Max(a, b), Math.Max(c, d)); }
Is this for a school assignment? :-DLuis Alonso Ramos Intelectix Chihuahua, Mexico
Not much here: My CP Blog!
-
Hi all, i am new to programming in C#. i dont know to code it to get the output ie., the largest among the four numbers and the user will give the input as numbers. and the munbers are int type inta,b,c,d; i need the code for largest among the four numbers of int a,b,c,d. pleae help me in sending code. regards, chitra.
i'd use: int[] numbers = {a,b,c,d}; int MaxNumber = GetHighestNumber(numbers); public int GetHighestNumber(int[] testNumbers) { int max = 0; foreach(int i in testNumbers) { //algorithm goes here } return max; }
-
Hi all, i am new to programming in C#. i dont know to code it to get the output ie., the largest among the four numbers and the user will give the input as numbers. and the munbers are int type inta,b,c,d; i need the code for largest among the four numbers of int a,b,c,d. pleae help me in sending code. regards, chitra.
Using extension methods (C# 3.0):
int[] nums = {a,b,c,d}; int max = nums.GetLargest();
:cool:
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
There's a simpler algorithm:
int Max4(int a, int b, int c, int d) { return Math.Max(Math.Max(a, b), Math.Max(c, d)); }
Is this for a school assignment? :-DLuis Alonso Ramos Intelectix Chihuahua, Mexico
Not much here: My CP Blog!
-
Luis Alonso Ramos wrote:
Is this for a school assignment?
Seems pretty likely - and you just did him no favours by doing it for him.
Yeah, I didn't notice until I saw other posts from this same guy... :doh: Anyway, the solution was trivial if he knew of the existence of
Math.Max
.Luis Alonso Ramos Intelectix Chihuahua, Mexico
Not much here: My CP Blog!