settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Program which finds the biggest of five (5) numbers in C# (with if statements)

+4 votes

I need to write a program that finds the biggest of five numbers by using only five if statements.

Examples:

a

b

c

d

e

biggest

5

2

2

4

1

5

-2

-22

1

0

0

1

-2

4

3

2

0

4

0

-2.5

0

5

5

5

-3

-0.5

-1.1

-2

-0.1

-0.1

 

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

2 Answers

+1 vote

Try this code:

using System;

class TheBiggestOfFiveNumbers
{
    static void Main()
    {
        Console.WriteLine("Please write 5 numbers:");
        decimal a = decimal.Parse(Console.ReadLine());
        decimal b = decimal.Parse(Console.ReadLine());
        decimal c = decimal.Parse(Console.ReadLine());
        decimal d = decimal.Parse(Console.ReadLine());
        decimal e = decimal.Parse(Console.ReadLine());

        if ((a >= b) && (a >= c) && (a >= d) && (a >= e))
        {
            Console.WriteLine("The biggest number is: {0}", a);
            return;
        }
        if ((b >= a) && (b >= c) && (b >= d) && (b >= c))
        {
            Console.WriteLine("The biggest number is: {0}", b);
            return;
        }
        if ((c >= a) && (c >= b) && (c >= d) && (c >= e))
        {
            Console.WriteLine("The biggest number is: {0}", c);
            return;
        }
        if ((d >= a) && (d >= b) && (d >= c) && (d >= e))
        {
            Console.WriteLine("The biggest number is: {0}", d);
            return;
        }
        if ((e >= a) && (e >= b) && (e >= c) && (e >= d))
        {
            Console.WriteLine("The biggest number is: {0}", e);
            return;
        }
    }
}
answered by user sam
edited by user golearnweb
0 votes

Or you can do it with an array and use on it the method .Max();

Like this:

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 d = decimal.Parse(Console.ReadLine());
        decimal e = decimal.Parse(Console.ReadLine());

        decimal[] arrayNumbers = { a, b, c, d, e };
        decimal arrayBiggest = arrayNumbers.Max();

        Console.WriteLine(arrayBiggest);
    }
}

 

answered by user golearnweb
...