[Solved] Exchange if greater (task in C#)

  

2
Topic starter

Write an if-statement that takes two integer variables a and b and exchanges their values if the first one is greater than the second one. As a result print the values a and b, separated by a space.

Examples:

exchange if greater task in C#

1 Answer
2

Here's how I did this task:

using System;
 
class ExchangeIfGreater
{
    static void Main()
    {
        decimal a = decimal.Parse(Console.ReadLine());
        decimal b = decimal.Parse(Console.ReadLine());
 
        if (a > b)
        {
            Console.WriteLine("{0} {1}", b, a);
        }
        else
        {
            Console.WriteLine("{0} {1}", a, b);
        }
    }
}
Share: