settingsAccountsettings
Menusettings

Q: Finding the biggest of three (3) numbers in C#

+7 votes

I need to write a program that finds the biggest number from given 3 numbers and print the result on the console in C#.

How can I do it?

asked in C# category by user paulcabalit
retagged by user golearnweb

4 Answers

+1 vote

Try this code out:

using System;

class BiggestNumber
{
    static void Main()
    {
        int a = int.Parse(Console.ReadLine());
        int b = int.Parse(Console.ReadLine());
        int c = int.Parse(Console.ReadLine());

        if (a > b && a > c)
        {
            Console.WriteLine("The biggest number is: {0}", a);
        }
        else if (b > a && b > c)
        {
            Console.WriteLine("The biggest number is: {0}", b);
        }
        else
        {
            Console.WriteLine("The biggest number is: {0}", c);
        }
    }
}
answered by user samfred5830
edited by user golearnweb
+1 vote

Here is my answer:

using System;

class BigNumber
{
    static void Main()
    {
        int a = int.Parse(Console.ReadLine());
        int b = int.Parse(Console.ReadLine());
        int c = int.Parse(Console.ReadLine());

        if (a > b)
        {
            if (a > c)
            {
                Console.WriteLine("The biggest number is: {0}", a);
            }
            else
            {
                Console.WriteLine("The biggest number is: {0}", c);
            }
        }
        else
        {
            if (b > c)
            {
                Console.WriteLine("The biggest number is: {0}", b);
            }
            else
            {
                Console.WriteLine("The biggest number is: {0}", c);
            }
        }
    }
}
answered by user john7
edited by user golearnweb
+1 vote

You must be careful with the numerical data type! For example if you have an output like this:

a

b

c

biggest

5

2

2

5

-2

-2

1

1

-2

4

3

4

0

-2.5

5

5

-0.1

-0.5

-1.1

-0.1

 

Then you must use double (or decimal) instead of integer... Like this:

using System;

class TheBiggestOfThreeNumbers
{
    static void Main()
    {
        Console.WriteLine("Please enter 3 numbers: ");
        double a = double.Parse(Console.ReadLine());
        double b = double.Parse(Console.ReadLine());
        double c = double.Parse(Console.ReadLine());

        if (a>b && a>c)
        {
            Console.WriteLine("The biggest number is: " + a);
        }
        else if (b>a && b>c)
        {
            Console.WriteLine("The biggest number is: " + b);
        }
        else
        {
            Console.WriteLine("The biggest number is: " + c);
        }
    }
}
answered by user mitko
edited by user golearnweb
Or like this:
using System;
 
namespace TheBiggestOfThreeNumbers
{
    class TheBiggestOfThreeNumbers
    {
        static void Main()
        {
            double a = double.Parse(Console.ReadLine());
            double b = double.Parse(Console.ReadLine());
            double c = double.Parse(Console.ReadLine());
 
            if (a > b)
            {
                if (a > c)
                {
                    Console.WriteLine("{0}", a);
                }
            }
            else if (b > a)
            {
                if (b > c)
                {
                    Console.WriteLine("{0}", b);
                }
            }
            else if (c > a)
            {
                if (c > b)
                {
                    Console.WriteLine("{0}", c);
                }
            }
        }
    }
}
0 votes

After you have the numbers - you can put them in an array (first make sure you have this line in the beginning of the code: using System.Linq;) - and then to use one of the functions of the array - .Min (or in this case .Max)  - easy and very useful!

The code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class Task
{
    static void Main()
    {
        decimal a = decimal.Parse(Console.ReadLine());
        decimal b = decimal.Parse(Console.ReadLine());
        decimal c = decimal.Parse(Console.ReadLine());

        decimal[] arrayNumbers = { a, b, c };
        decimal arrayNumbersBiggest = arrayNumbers.Max();
        Console.WriteLine(arrayNumbersBiggest);
    }
}
answered by user golearnweb
edited by user golearnweb
...