Java
Slip2
Q.1) Write a java program to accept n names of cites from user and display them in descending order. [Marks 30]
Solution
import java.io.*;
class slip2
{
public static void main(String args[]) throws IOException
{
String str[]=new String[5];
String temp;
Scanner sr=new Scanner(System.in);
System.out.println("Enter the name of 5 cities");
for(int i=0;i<5;i++)
{
str[i]=sr.next();
}
System.out.println("After sorting the order is:");
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
{
if(str[j].compareTo(str[i])>0)
{
String tmp=str[j];
str[j]=str[i];
str[i]=tmp;
}
}
}
for(int i=0;i<5;i++)
System.out.println(str[i]);
}
}
Tags:
java