Assume that you have three variables named v1 v2 and v3 Writ
Assume that you have three variables named v1, v2, and v3. Write the code necessary to print out their three values in order from low to high. You may not use an array nor any sorting algorithm. Just standard conditionals (if-else-if). ( using java language)
Solution
SortThree.java
import java.util.Scanner;
public class SortThree {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter first number: \");
int v1 = scan.nextInt();
System.out.println(\"Enter second number: \");
int v2 = scan.nextInt();
System.out.println(\"Enter third number: \");
int v3 = scan.nextInt();
if ((v1 > v2 && v1 > v3))
{
if(v2 > v3)
{
System.out.print(v3 + \" \" + v2 + \" \" + v1);
}
else
System.out.print(v2 + \" \" + v3 + \" \" + v1);
}
else if ((v2 > v1 && v2 > v3))
{
if(v1 > v3)
{
System.out.print(v3 + \" \" + v1 + \" \" + v1);
}
else
{
System.out.print(v1 + \" \" + v3 + \" \" + v2);
}
}
else if ((v3 > v1 && v3 > v2))
{
if(v1 > v2)
{
System.out.print(v2 + \" \" + v1 + \" \" + v3);
}
else
System.out.print(v1 + \" \" + v2 + \" \" + v3);
}
}
}
Output:
Enter first number:
5
Enter second number:
4
Enter third number:
3
3 4 5

