answersLogoWhite

0


Best Answer

The cheap way out, just use what java has available: the Arrays class (of course you have to import it first). int[] array = {42, 12, 1337, 0}; // the four numbers to be sorted Arrays.sort(array); //Now array is sorted

// array should now contain {0, 12, 42, 1337}; Of course there are other ways to sort, especially writing algorithms yourself, but that's such a bore. Just for reference I have attached links to common sorts below.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

import java.util.*;

public class ascendingorder

{

static Scanner console = new Scanner(System.in);

public static void main(String[]args)

{

int x,y,z;

System.out.print("Enter the first number: " );

x = console.nextInt();

System.out.println();

System.out.print("Enter the second number: " );

y = console.nextInt();

System.out.println();

System.out.print("Enter the third number: " );

z = console.nextInt();

System.out.println();

if ((x<y) && (x<z))

{

System.out.print("Numbers in ascending order are: " +x );

if (y<z)

System.out.println(" "+ y + " " + z );

else

System.out.println(" " + z + " "+ y );

}

else

if ((y<x) && (y<z))

{

System.out.print("Numbers in ascending order are: " +y );

if (x<z)

System.out.println(" "+ x + " " + z );

else

System.out.println(" " + z + " "+ x );

}

else

if ((z<x) && (z<y))

{

System.out.print("Numbers in ascending order are: " +z );

if (x<y)

System.out.println(" "+ x + " " + y );

else

System.out.println(" " + y + " "+ x );

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

First of all I would just like to say that if this is a coursework assignment YOU WILL GET CAUGHT COPYING THIS! So please don't for your sake..

We shall call the program Sort3 yes?

*START OF PROGRAM*

public class Sort3

{

public static void main(String[] args)

{

//obtain the three values from the command line

int firstNo = Integer.parseInt(args[0]);

int secondNo = Integer.parseInt(args[1]);

int thirdNo = Integer.parseInt(args[2]);

//put the values into the sort method

sort(firstNo, secondNo, thirdNo);

} //main

//parameters are for three integer values

private static void sort(int first, int second, int third)

{

if (first > second && first > third && second > third)

System.out.println(first + " " + second + " " + third);

else if (first > second && first > third && second < third)

System.out.println(first + " " + third + " " + second);

else if (second > first && second > third && first > third)

System.out.println(second + " " + first + " " + third);

else if (second > first && second > third && first < third)

System.out.println(second + " " + third + " " + first);

else if (third > first && third > second && second > first)

System.out.println(third + " " + second + " " + first);

else if (third > first && third > second && second < first)

System.out.println(third + " " + first + " " + second);

} //sort

}

*END OF PROGRAM*

save the file as "Sort3.java" then in the command line (whilst in the directory where the file was saved) run "javac Sort3.java". This will compile it. to use it, do "java Sort3 No1 No2 No3" where you replace No1 No2 and No3 with the numbers you want to sort. :D

There is a more advanced way of doing this which could handle any amount of numbers to sort but is longer and this does the trick anyway.

u should also put the greater than and equal to signs in the if statements so that numbers of equal value will be sorted out properly

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Easy.

int num1;

int num2;

int num3;

int num4;

int greatest = Math.max(Math.max(num1,num2),Math.max(num3,num4));

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Java program to find greatest among 4 numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What Program that will display the even numbers in data structures and algorithms?

JAVA


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


Java program to find sum on even numbers from 12-45?

sum = 0; for (int i = 12; i


What is java virtucal mechine?

That refers to the program that runs the compiled Java program.


What is a Java applet?

PCH answer small Embedded Java Program.

Related questions

What Program that will display the even numbers in data structures and algorithms?

JAVA


How do you compile and execute a java program which finds out the highest of any five numbers?

it will be destroyed.


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


What does it mean to generate random numbers in Java?

Generating random numbers in Java is somewhat of a misnomer because the numbers are actually semi-random.It means to use the program to obtain random integers to use in hypothetical situations such as statistics.


Java program to find sum on even numbers from 12-45?

sum = 0; for (int i = 12; i


What is java virtucal mechine?

That refers to the program that runs the compiled Java program.


What is java program on computers?

#!/usr/bin/perl print 'java program';


What powder game doesnt need java?

Any program written for the Java technology needs Java. Any program NOT written for Java DOESN'T need it.


How do you write and execute java program?

In theory, you can write a Java program in just about any text editor; then you can compile it with the "javac" command, and - if the compilation works without errors - run it with the "java" command. In practice, and especially for new programmers, I would recommend that you get a good Java IDE, which lets you do everything in a single environment, and clearly shows where there are errors, among other benefits.


How do you write and execute the java program?

In theory, you can write a Java program in just about any text editor; then you can compile it with the "javac" command, and - if the compilation works without errors - run it with the "java" command. In practice, and especially for new programmers, I would recommend that you get a good Java IDE, which lets you do everything in a single environment, and clearly shows where there are errors, among other benefits.


Java Track?

Java Track is the java training program offered by SynergisticIT


Where can one get the program Java?

The program Java can be downloaded from the official Java website on the internet. The program is free for personal use, so it can be downloaded without any cost by anyone.