In: Computer Science
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
C# code:
using System;
class Program{
static void Main() {
//accepting first number
int num1=Convert.ToInt32(Console.ReadLine());
//accepting second number
int num2=Convert.ToInt32(Console.ReadLine());
//accepting third number
int num3=Convert.ToInt32(Console.ReadLine());
//checking if num1 is the smallest
if(num1<num2 && num1<num3){
//printing num1
Console.Write(num1+" ");
if(num2<num3){
//printing num2
Console.Write(num2+" ");
//printing num3
Console.Write(num3);
}
else{
//printing num3
Console.Write(num3+" ");
//printing num2
Console.Write(num2);
}
}
//checking if num2 is the smallest
if(num2<num1 && num2<num3){
//printing num2
Console.Write(num2+" ");
if(num1<num3){
//printing num1
Console.Write(num1+" ");
//printing num3
Console.Write(num3);
}
else{
//printing num3
Console.Write(num3+" ");
//printing num1
Console.Write(num1);
}
}
//checking if num3 is the smallest
if(num3<num1 && num3<num1){
//printing num3
Console.Write(num3+" ");
if(num2<num1){
//printing num2
Console.Write(num2+" ");
//printing num1
Console.Write(num1);
}
else{
//printing num1
Console.Write(num1+" ");
//printing num2
Console.Write(num2);
}
}
}
}
Screenshot:
Input :
Output: