settingsAccountsettings
Menusettings

Q: I need to sort 3 numbers with nested ifs statements in C#

+4 votes

I need to write a program that enters 3 real numbers and prints them sorted in descending order (from bigger to smaller).

Nested if statements must be used. I don’t have to use arrays and the built-in sorting functionality.

Here are some examples:

a

b

c

result

5

1

2

5 2 1

-2

-2

1

1 -2 -2

-2

4

3

4 3 -2

0

-2.5

5

5 0 -2.5

-1.1

-0.5

-0.1

-0.1 -0.5 -1.1

10

20

30

30 20 10

1

1

1

1 1 1

 

asked in C# category by user samfred5830

2 Answers

+1 vote
Try this code:
 
 using System;
 
namespace Sort3NumbersWithNestedIfs
{
    class Sort3NumbersWithNetsedIfs
    {
        static void Main()
        {
            double a = double.Parse(Console.ReadLine());
            double b = double.Parse(Console.ReadLine());
            double c = double.Parse(Console.ReadLine());
 
            if ((a > b) && (a > c))
            {
                if (b > c)
                {
                    Console.WriteLine("{0} {1} {2}", a, b, c);
                }
                else
                {
                    Console.WriteLine("{0} {1} {2}", a, c, b);
                }
            }
            else if ((b > a) && (b > c))
            {
                if (a > c)
                {
                    Console.WriteLine("{0} {1} {2}", b, a, c);
                }
                else
                {
                    Console.WriteLine("{0} {1} {2}", b, c, a);
                }
            }
            else if ((c > a) && (c > b))
            {
                if (a > b)
                {
                    Console.WriteLine("{0} {1} {2}", c, a, b);
                }
                else
                {
                    Console.WriteLine("{0} {1} {2}", c, b, a);
                }
            }
        }
    }
}
answered by user john7
edited by user golearnweb
+1 vote

Or this one:

using System;

class NumbersWithNestedIfs
{
    static void Main()
    {
        Console.WriteLine("Please write 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)
        {
            if (b>c)
            {
                Console.WriteLine("Descending order A: " + a + " " + b + " " + c);
            }
            else
            {
                Console.WriteLine("Descending order A1: " + a + " " + c + " " + b);
            }
        }
        if (b>a && b>c)
        {
            if (a>c)
            {
                Console.WriteLine("Descending order B: " + b + " " + a + " " +c);
            }
            else
            {
                Console.WriteLine("Descending order B1: " + b + " " + c + " " +a);
            }
        }
        if (c>a && c>b)
        {
            if (a>b)
            {
                Console.WriteLine("Descending order C: " + c + " " + a + " " + b);
            }
            else
            {
                Console.WriteLine("Descending order C1: " + c + " " + b + " " + a);
            }
        }
    }
}
answered by user hues
edited by user golearnweb
...