0% found this document useful (0 votes)
8 views14 pages

1

The document contains several Java programs that demonstrate different functionalities such as sorting student details by register number, sorting city populations, searching for wonders by name, sorting student details by height, and performing a binary search on a list of years. Each program utilizes arrays and loops to manage and manipulate data input by the user. The programs are designed to showcase basic programming concepts including user input, sorting algorithms, and searching techniques.

Uploaded by

ancheloarun87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

1

The document contains several Java programs that demonstrate different functionalities such as sorting student details by register number, sorting city populations, searching for wonders by name, sorting student details by height, and performing a binary search on a list of years. Each program utilizes arrays and loops to manage and manipulate data input by the user. The programs are designed to showcase basic programming concepts including user input, sorting algorithms, and searching techniques.

Uploaded by

ancheloarun87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

3. import java.util.

Scanner;

public class x {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.print("Enter the total number of students: ");


int n = in.nextInt();
in.nextLine();

String[] s = new String[n];


int[] rno = new int[n];
int[] mark = new int[n];

for (int i = 0; i < n; i++) {


System.out.print("Enter the name of student " + (i + 1) + ": ");
s[i] = in.nextLine();
System.out.print("Enter the register number of student " + (i + 1) + ": ");
rno[i] = in.nextInt();
System.out.print("Enter the English mark of student " + (i + 1) + ": ");
mark[i] = in.nextInt();
in.nextLine();
}

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - 1 - i; j++) {
if (rno[j] > rno[j + 1]) {
int tempRno = rno[j];
rno[j] = rno[j + 1];
rno[j + 1] = tempRno;

String tempS = s[j];


s[j] = s[j + 1];
s[j + 1] = tempS;

int tempMark = mark[j];


mark[j] = mark[j + 1];
mark[j + 1] = tempMark;
}
}
}

System.out.println("The details sorted by register number are:");


for (int i = 0; i < n; i++) {
System.out.println("Register Number: " + rno[i] + ", Student: " + s[i] + ",
English Mark: " + mark[i]);
}
}
}
1. import java.util.*;

class CityPopulation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] cities = new String[10];
int[] populations = new int[10];
System.out.println("enter cities and its populations");
for (int i = 0; i < 10; i++) {
cities[i] = sc.next();
populations[i] = sc.nextInt();
}

for (int i = 0; i < populations.length - 1; i++) {


int maxIndex = i;
for (int j = i + 1; j < populations.length; j++) {
if (populations[j] > populations[maxIndex]) {
maxIndex = j;
}
}
int tempPop = populations[i];
populations[i] = populations[maxIndex];
populations[maxIndex] = tempPop;

String tempCity = cities[i];


cities[i] = cities[maxIndex];
cities[maxIndex] = tempCity;
}

for (int i = 0; i < 10; i++) {


System.out.println(cities[i] + " " + populations[i]);
}
}
}
2. import java.util.Scanner;

class x {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

String[] w = { "Great Wall of China", "Christ the Redeemer", "Machu


Picchu",
"Chichen Itza", "Roman Colosseum", "Taj Mahal", "Petra" };

String[] c = { "China", "Brazil", "Peru", "Mexico", "Italy", "India", "Jordan" };

System.out.print("Enter the wonder to be searched: ");


String ns = in.nextLine();
int k = 0;

for (int i = 0; i < 7; i++) {


if (w[i].equalsIgnoreCase(ns)) {
k = 1;
System.out.println("Wonder: " + w[i] + ", Country: " + c[i]);
break;
}
}

if (k == 0)
System.out.println(" not found.");
}
}
4. import java.util.*;

class X {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] n = new String[10];
int[] a = new int[10];
int[] h = new int[10];

for (int i = 0; i < 10; i++) {


System.out.print("Enter name of student ");
n[i] = in.next();
System.out.print("Enter age of student ");
a[i] = in.nextInt();
System.out.print("Enter height of student");
h[i] = in.nextInt();
}

for (int i = 0; i < 9; i++) {


int min = i;
for (int j = i + 1; j < 10; j++) {
if (h[j] > h[min])
min = j;
}

int t = h[i];
h[i] = h[min];
h[min] = t;

String tempName = n[i];


n[i] = n[min];
n[min] = tempName;

int tempAge = a[i];


a[i] = a[min];
a[min] = tempAge;
}
System.out.println("Details after sorting ");
for (int i = 0; i < 10; i++) {
System.out.println("Name: " + n[i] + ", Age: " + a[i] + ", Height: " + h[i]);
}
}
}
5. import java.util.Scanner;

class X {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] year = {1982, 1988, 1998, 2002, 2006, 2007, 2012, 2014};
int lb = 0;
int ub = 7;
int k = 0;

System.out.print("Enter the year to be searched: ");


int ns = in.nextInt();

while (lb <= ub) {


int p = (lb + ub) / 2;

if (year[p] < ns)


lb = p + 1;
else if (year[p] > ns)
ub = p - 1;

else {
k = 1;
break;
}
}

if (k == 1)
System.out.println("The search is successful and the year is present");
else
System.out.println("Record does not exist");
}
}

You might also like