Advertisements
Advertisements
प्रश्न
Write a program for the following.
Create a String array WR[ ] and an integer array WL[], both of size 10. Input/Store names of World Rivers. Arrange the River Names in ascending order of their lengths.
कोड लेखन
Advertisements
उत्तर
import java.util.*;
class clTask
{
void main()
{
Scanner sc = new Scanner(System.in);
String WR[] = new String [10];
int WL[] = new int[10];
for(int { j = 0; j <= 9; j++)
{
System.out.print("Enter River num" +j+" :");
WR[j] = sc.nextLine();
System.out.print("Enter its length ");
WL[j] = sc.nextInt();
String buf = sc.nextLine(); // to clear buffer
}
for(int j = 0; j <= 8; j++)
{
for(int k = 0; k <= 8 − j; k++)
{
if(WL[k] > WL[k + 1])
{
int tmpi = WL[k];
WL[k] = WL[k + 1];
WL[k + 1] = tmpi;
String tmps = WR[k];
WR[k] = WR[k + 1];
WR[k + 1] = tmps;
}
}
}
System.out.println("In Ascending Order of size: ");
for(int j = 0; j<=9; j++)
{
System.out.println("River : " + WR[j] +" Length:" + WL[j]);
}
}
}
Output:
| Enter River num 0 | IRTYSH |
| Enter its length 5410 | |
| Enter River num 1 | MISSISSIPI |
| Enter its length 6275 | |
| Enter River num 2 | ARGUN |
| Enter its length 4444 | |
| Enter River num 3 | YELLOW |
| Enter its length 5464 | |
| Enter River num 4 | YANGTZE |
| Enter its length 6300 | |
| Enter River num 5 | LENA |
| Enter its length 4400 | |
| Enter River num 6 | CHAMBESHI |
| Enter its length 4700 | |
| Enter River num 7 | NILE |
| Enter its length 6650 | |
| Enter River num 8 | YENISEI |
| Enter its length 5539 | |
| Enter River num 9 | AMAZON |
| Enter its length 6400 | |
| In Ascending Order of size: | |||
| River | LENA | Length | 4400 |
| River | ARGUN | Length | 4444 |
| River | CHAMBESHI | Length | 4700 |
| River | IRTYSH | Length | 5410 |
| River | YELLOW | Length | 5464 |
| River | YENISEI | Length | 5539 |
| River | MISSISSIРІ | Length | 6275 |
| River | YANGTZE | Length | 6300 |
| River | AMAZON | Length | 6400 |
| River | NILE | Length | 4450 |
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 8: Arrays - Sort & Search - Exercises [पृष्ठ २००]
