# Need Help Displaying the largest Number

Discussion in 'C#' started by Travis876, Sep 27, 2008.

1. ### Travis876New Member

Joined:
Sep 27, 2008
Messages:
2
0
Trophy Points:
0
Hey guys my name is Travis and Im new at C#. I need help writing a code that will display the largest number when the user enters any three random number. I have asked a few friends and they recmonded a bubble sort but that is over my head. This is what I have so far for my code but it doesnt work. I can post the rest of my code later if that would also help. But this is the part Im stuck on:

//determines the largest number
largestNum = number1;
Console.WriteLine("The largest number is {0}", number1);

if (number2 >= largestNum)
Console.WriteLine("The largest number is {0}", largestNum);

if (number3 >= largestNum)
Console.WriteLine("The largest number is {0}", largestNum);

Thanks for any advice and suggestions.

2. ### Travis876New Member

Joined:
Sep 27, 2008
Messages:
2
0
Trophy Points:
0
Anybody have anything.

3. ### NewsBotNew Member

Joined:
Dec 2, 2008
Messages:
1,267
2
Trophy Points:
0
if (number2 >= largestNum) then number2 is larger. Dont you think so

4. ### medinkNew Member

Joined:
Jan 22, 2009
Messages:
2
0
Trophy Points:
0
I wouldn't do it like that, because number2 can be larger than numer1, which causes you to write out that number2 is the largest number.

After that, number3 might be larger than number2, which causes you to write out that number3 is the largest number.
Instead, save their numbers in an array and loop around.

int largestNum = 0;
int[] numbers = new int[3];
for (int i = 0; i < 3; i++)
{
if (numbers > largestNum)
largestNum = numbers;
}
Console.WriteLine("The largest number is {0}", largestNum);

Hope this works for you, let me know if there is anything else

5. ### johny_1015_1981New Member

Joined:
Oct 5, 2008
Messages:
19
0
Trophy Points:
0
try this
solution 1:
if (number2>number1)
largestnum=number2;
else largestnum=number1;

if (largestnum<number3)
largestnum=number3;

Console.WriteLine("The Largest number is {0}", largestnum);

//next thing is stop asking start thinking, this is a logical question. I believe logical question must have to be solved by yourself and dont let bubble sort fly over your head beacuse it is the easiest sorting solution and ofcourse not a good solution to find the largest number;

//solution 2

number; as array
largnum = number[0];
for(i=1;number.length<i;i=i+1)
{
if (number>largnum)
{
largnum = number;
}
}

Console.writeline("largest number {0}", largnum);

//there might be some syntex error write the solid program according to the syntex

6. ### StatementNew Member

Joined:
Mar 12, 2008
Messages:
5
0
Trophy Points:
0
Use Enumerable.Max(); if you put the numbers in a list.
Here's some example code.

Code:
```using System;
using System.Collections.Generic;
using System.Linq;

namespace NumberCrunch
{
class Program
{
static void Main(string[] args)
{
var numbers = new List<int>();

var tries = 3;
for (var i = 0; i < tries; )
{
try
{
Console.Write("Enter a number ({0}/{1}): ", i + 1, tries);
var number = int.Parse(input);
++i;
}
catch (FormatException)
{
// A format exception just means in this case that the user wrote some
Console.WriteLine("You must specify a proper number, like 42 or -103!");
}
}

Console.WriteLine("You wrote these numbers:");
foreach (var number in numbers)
{
Console.WriteLine("\t{0}", number);
}

var highestNumber = numbers.Max();
Console.WriteLine("The highest number you wrote:");
Console.WriteLine("\t{0}", highestNumber);
}
}
}```