Programs
on Array
1.
Write a program to input 15 integer elements
in an array and sort them in ascending order using the bubble sort technique.
import java.util.Scanner;
class Array2019
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int size=15;
int a[] = new int[size];
System.out.println(" Please enter
15 integers:");
for(int i = 0; i < size; i++)
a[i] = sc.nextInt();
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size - 1 - i;
j++)
{
if(a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.println("Sorted
elements are:");
for(int i = 0; i < size; i++)
System.out.print(a[i] + "
");
}
}
2.
Write a program to accept name and total marks
of N number of students in two single subscripts array name[] and totalmarks[].
Calculate and print:
(i) The average of the total marks obtained by N number of
students.
[average = (sum of total marks of all the students) / N]
(ii) Deviation of each student’s total marks with the
average.
[deviation = total marks of a student – average]
import java.util.Scanner;
class Array2018
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter
Number of students: ");
int n = sc.nextInt();
sc.nextLine(); // Consuming the left
over line after integer in Scanner
String name[] = new String[n];
int totalmarks[] = new int[n];
int sum=0;
double avg = 0.0;
double dev = 0.0;
for(int i = 0; i < n; i++){
System.out.print("Please enter
Name: ");
name[i] = sc.nextLine();
System.out.print("Please enter
Marks: ");
totalmarks[i] = sc.nextInt();
sc.nextLine(); // Consuming the
left over line after integer in Scanner
sum += totalmarks[i];
}
avg = (double)sum / n;
for(int i = 0; i < n; i++){
dev = totalmarks[i] - avg;
System.out.println("Deviation
for student " + name[i] + ": " + dev);
}
System.out.println("Average score:
" + avg);
}
}
3.
Write a
program to input integer elements into an array of size 20 and perform the
following operations:
a.
Display
the largest number from the array.
b.
Display
the smallest number from the array.
c.
Display
sum of all the elements of the array.
import java.util.Scanner;
class Array2017_1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[] = new int[20];
int sum = 0;
int largest = 0;
int smallest = 0;
System.out.println("Enter 20
elements:");
for(int i = 0; i < 20; i++)
{
a[i] = sc.nextInt();
if(i == 0)
largest = smallest = a[0];
if(largest < a[i])
largest = a[i];
if(smallest > a[i])
smallest = a[i];
sum += a[i];
}
System.out.println("Sum = " +
sum);
System.out.println("Largest number
is " + largest);
System.out.println("Smallest
number is " + smallest);
}
}
4.
Write a
program to input forty words in an array. Arrange these words in descending
order of alphabets, using selection sort technique. Print the sorted array.
import java.util.Scanner;
class Array2017_2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String words[] = new String[40];
System.out.println("Please Enter
40 words: ");
int size=words.length;
for(int i = 0; i < size; i++)
words[i] = sc.nextLine();
for(int i = 0; i < size; i++){
int max = i;
for(int j = i + 1; j < size;
j++)
{
if(words[max].compareTo(words[j])
< 0)
{
max = j;
}
}
String temp = words[i];
words[i] = words[max];
words[max] = temp;
}
for(int i = 0; i < size; i++)
System.out.print(words[i] + "
");
}
}
5.
Write a
program to initialize the seven Wonders of the World along with their locations
in two different arrays. Search for a name of the country input by the user. If
found, display the name of the country along with its Wonder, otherwise display
“Sorry not found!”.
Seven Wonders: CHICHEN ITZA, CHRIST THE REDEEMER, TAJ MAHAL,
GREAT WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM
Locations: MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Examples:
Country name: INDIA
Output: TAJ MAHAL
Country name: USA
Output: Sorry not found!
import java.util.Scanner;
class Array2016
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String wonders[] = {"CHICHEN
ITZA", "CHRIST THE REDEEMER", "TAJ MAHAL",
"GREAT WALL OF CHINA",
"MACHU PICCHU", "PETRA", "COLOSSEUM"};
String location[] =
{"MEXICO", "BRAZIL", "INDIA", "CHINA",
"PERU", "JORDAN", "ITALY"};
System.out.print("Please enter any
Country name: ");
String country = sc.nextLine();
int i = 0,flag=0;
for(i=0; i < wonders.length; i++)
{
if(country.equalsIgnoreCase(location[i]))
{
System.out.println(wonders[i]);
flag=1;
break;
}
}
if(flag==0)
System.out.println("Sorry not
found!");
}
}
6.
Write a
program to input and store roll numbers, names and marks in 3 subjects of n
number of students in five single-dimensional arrays and display the remark
based on average marks as given below:
Average marks = Total Marks ÷ 3
Average Marks
Remark
85 - 100 EXCELLENT
75 - 84 DISTINCTION
60 - 74 FIRST CLASS
40 - 59 PASS
Less than 40 POOR
The maximum marks in the subject are 100.
import java.util.Scanner;
class Array2015_1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter
number of students: ");
int n = sc.nextInt();
int rollNo[] = new int[n];
String name[] = new String[n];
int marks1[] = new int[n];
int marks2[] = new int[n];
int marks3[] = new int[n];
double total=0.0;
double avg[] = new double[n];
for(int i = 0; i < n; i++)
{
System.out.print("Please enter
Roll number: ");
rollNo[i] = sc.nextInt();
sc.nextLine(); // Consuming the
left over line after integer in Scanner
System.out.print("Please enter
Name: ");
name[i] = sc.nextLine();
System.out.print("Please enter
Marks 1 out of 100: ");
marks1[i] = sc.nextInt();
sc.nextLine(); // Consuming the
left over line after integer in Scanner
System.out.print("Please enter
Marks 2 out of 100: ");
marks2[i] = sc.nextInt();
sc.nextLine(); // Consuming the
left over line after integer in Scanner
System.out.print("Please enter Marks
3 out of 100: ");
marks3[i] = sc.nextInt();
sc.nextLine(); // Consuming the
left over line after integer in Scanner
total = marks1[i] + marks2[i] +
marks3[i];
avg[i] = total / 3.0;
}
String remark="";
System.out.println("RollNo\tName\tMarks1\tMarks2\tMarks3\tRemarks
");
for(int i = 0; i < n; i++)
{
if(avg[i] >= 85 &&
avg[i] <= 100)
remark="EXCELLENT";
else if(avg[i] >= 75 &&
avg[i] < 85)
remark="DISTINCTION";
else if(avg[i] >= 60 &&
avg[i] < 75)
remark="FIRST CLASS";
else if(avg[i] >= 40 &&
avg[i] < 60)
remark="PASS";
else
remark="POOR";
System.out.println(rollNo[i]+
"\t"+name[i]+"\t"+marks1[i]+"\t"
+marks2[i]+"\t"+
marks3[i]+"\t" +remark);
}
}
}
7.
Write a
program to input twenty names in an array. Arrange these names in descending
order of alphabets , using the bubble sort technique.
import java.util.Scanner;
public class Array2015
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String[] names20 = new String[20];
int l=20;//int l= names20.length;
int i=0,j=0;
System.out.println("Please enter 20 names");
for (i = 0; i < l; i++)
{
names20[i] = sc.nextLine();
}
for (i = 0; i < l; i++)
{
for (j = 0; j < l-1-i; j++)
{
if (names20[j].compareTo(names20[j+1])
> 0)
{
String temp = names20[j];
names20[j] = names20[j+1];
names20[j+1] = temp;
}
}
}
System.out.println("Sorted names: ");
for (i = 0; i < l; i++)
{
System.out.println(names20[i]);
}
}
}
8.
Write a
program to accept the year of graduation from school as an integer value from
the user. Using the binary search technique on the sorted array of integers
given below, output the message “Record exists” if the value input is located
in the array. If not, output the message “Record does not exist”. {1982, 1987, 1993,
1996, 1999, 2003, 2006, 2007, 2009, 2010}
import java.util.Scanner;
class Array2014
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[] = {1982, 1987, 1993, 1996,
1999, 2003, 2006, 2007, 2009, 2010};
System.out.print("Please enter
year of graduation: ");
int y = sc.nextInt();
int flag=0;
int low = 0;
int high = a.length - 1;
int mid = 0;
while(low <= high)
{
mid=(low + high) / 2;
if(y == a[mid])
{
flag=1;
System.out.println("Record
exists");
break;
}
else if(y < a[mid])
high = mid - 1;
else
low = mid + 1;
}
if(flag==0)
System.out.println("Record
does not exist");
}
}
9.
Write a
program to input 10 integer elements in an array and sort them in descending
order using bubble sort technique.
import java.util.Scanner;
class Array2013
{
public static void main(String args[])
{
Scanner sc = new
Scanner(System.in);
int arr[] = new int[10];
System.out.println("Please Enter
10 integers:");
int size=arr.length;
for(int i = 0; i < size; i++)
arr[i] = sc.nextInt();
for(int i = 0; i < size; i++){
for(int j = 0; j < size - 1 - i;
j++)
{
if(arr[j] < arr[j + 1]){
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Sorted
elements in descending order are - ");
for(int i = 0; i < size; i++)
System.out.print(arr[i] + "
");
}
}
10.
Write a
program to accept the names of 10 cities in a single dimension array and their
STD (Subscriber Trunk Dialing) codes in another single dimension integer array.
Search for a name of a city input by the user in the list. If found, display “Search
successful” and print the name of the city along with its STD code, or else display
the message “Search unsuccessful, no such city in the list”.
import java.util.Scanner;
class Array2012
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String name[] = new String[10];
int STD[] = new int[10];
int i = 0,flag=0;
for(i=0; i < 10; i++)
{
System.out.print(" Enter City:
");
name[i] = sc.nextLine();
System.out.print("Enter STD
code: ");
STD[i] = sc.nextInt();
sc.nextLine();
}
System.out.print("Please enter
City: ");
String city = sc.nextLine();
for(i = 0; i < 10; i++)
{
if(city.equalsIgnoreCase(name[i]))
{ flag=1;
System.out.println("Search
successful");
System.out.println(name[i] +
" - " + STD[i]);
break;
}
}
if(flag==0)
System.out.println("Search
unsuccessful, no such city in the list");
}
}
11.
Write a
program to input and sort the weight of ten people. Sort and display them in
descending order using the selection sort technique.
import java.util.Scanner;
class Array2011
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
double weight[] = new double[10];
System.out.println("Enter weights
of 10 people:");
for(int i = 0; i < weight.length;
i++)
weight[i] = sc.nextDouble();
for(int i = 0; i < weight.length;
i++){
int max = i;
for(int j = i + 1; j <
weight.length; j++){
if(weight[max]< weight[j])
{
max=j;
}
}
double temp = weight[i];
weight[i] = weight[max];
weight[max] = temp;
}
System.out.println("Weights in
descending sorted order :");
for(int i = 0; i < weight.length;
i++)
System.out.println(weight[i]);
}
}
12.
Write a
program to perform binary search on a list of integers given below, to search
for an element input by the user. If it is found, display the element along
with its position, otherwise display the message “Search element not found”. 5,
7, 9, 11, 15, 20, 30, 45, 89, 97.
import java.util.Scanner;
class Array2010
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int arr[] = {5, 7, 9, 11, 15, 20, 30,
45, 89, 97};
System.out.print("Element to
search: ");
int value = sc.nextInt();
int low = 0;
int high = arr.length - 1;
int flag=0,mid=0;
while(low <= high)
{
mid = (low + high) / 2;
if(value == arr[mid])
{
System.out.println(value + "
found at index " + mid);
flag=1;
break;
}
else if(value < arr[mid])
high = mid - 1;
else
low = mid + 1;
}
if(flag==0)
System.out.println("Search
element not found.");
}
}
13.
Shasha
Travels Pvt. Ltd. gives the following discount to its customers:
Ticket Amount Discount
Above Rs. 70000 18%
Rs. 55001 to Rs. 70000 16%
Rs. 35001 to Rs. 55000 12%
Rs. 25001 to Rs. 35000 10%
Less than Rs. 25001 2%
Write a program to input the name and ticket amount for the
customer and calculate the
discount amount and net amount to be paid.
Display the output in the following format for each
customer:
SNo. Name Ticket charges Discount
Net amount
____ ______ _______________ _________ ____________
Assume that there are 15 customers, first customer is given
the serial number 1,
next customer 2… and so on.
import java.util.Scanner;
class Display2010
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String name[] = new String[15];
double amt[] = new double[15];
double dp[] = new double[15];
double net[] = new double[15];
double da = 0.0;
for(int i = 0; i < 15; i++)
{
System.out.print("Please enter
Name: ");
name[i] = sc.nextLine();
System.out.print("Please enter
Ticket amount: ");
amt[i] = sc.nextDouble();
sc.nextLine();// Consuming the left over
line after integer in Scanner
da = 0.0;
if(amt[i] > 70000)
dp[i] = 18.0;
else if(amt[i] > 55000
&& amt[i] <= 70000)
dp[i] = 16.0;
else if(amt[i] > 35000
&& amt[i] <= 55000)
dp[i] = 12.0;
else if(amt[i] > 25000
&& amt[i] <= 35000)
dp[i] = 10.0;
else
dp[i] = 2.0;
da = dp[i] / 100.0 * amt[i];
net[i] = amt[i] - da;
}
System.out.println("SNo. Name
Ticket Charges Discount Net Amount");
for(int i = 0; i < 15; i++)
System.out.println((i + 1) +
"\t" + name[i] + "\t" + amt[i] + "\t" + dp[i] +
"%\t" + net[i]);
}
}
14.
Write a
program to store six elements in an array P, and four elements in an array Q
and produce a third array R, containing all elements of arrays P and
Q. Display the resultant array.
Example:
P[] = {4, 6, 1, 2, 3, 10}
Q[] = {19, 23, 7, 8}
R[] = {4, 6, 1, 2, 3, 10, 19, 23, 7, 8}
SOLUTION
import java.util.Scanner;
class Array2010_3
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int P[] = new int[6];
int Q[] = new int[4];
int R[] = new int[10];
System.out.println("Enter 6
elements in 1st array:");
for(int i = 0; i < 6; i++)
P[i] = sc.nextInt();
System.out.println("Enter 4
Elements in 2nd array:");
for(int i = 0; i < 4; i++)
Q[i] = sc.nextInt();
int pos = 0;
for(int i = 0; i < P.length; i++)
R[pos++] = P[i];
for(int i = 0; i < Q.length; i++)
R[pos++] = Q[i];
System.out.println("The resultant
array is: ");
for(int i = 0; i < R.length; i++)
System.out.print(R[i] + "
");
}
}
15.
An
electronics shop has announced the following seasonal discounts on the purchase
of certain items:
Purchase amount in Rs. Discount on Laptop Discount on Desktop PC
0 – 25000 0.0% 5.0%
25001 – 57000 5.0% 7.6%
57001 – 100000 7.5% 10%
More than 100000 10.0% 15.0%
Write a program based on the above criteria to input name,
address, amount of purchase and the type of purchase
(L for Laptop and D for Desktop) by a customer. Compute and
print the net amount to be paid by a customer along
with his name and address.
(Hint: Discount = (discount rate / 100) * amount of purchase,
Net amount = amount of purchase – discount)
import java.util.Scanner;
class Shop{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Name: ");
String name = sc.nextLine();
System.out.print("Address:
");
String address = sc.nextLine();
System.out.print("Amount of
purchase: ");
double amount = sc.nextDouble();
System.out.println("Please enter
type of purchase:");
System.out.print("L for Laptop and D
for Desktop");
char type = sc.next().charAt(0);
type=Character.toUpperCase(type);
double discount = 0.0;
if(amount <= 25000){
if(type == 'L')
discount = 0.0;
else if(type == 'D')
discount = 5.0;
}
else if(amount <= 57000){
if(type == 'L')
discount = 5.0;
else if(type == 'D')
discount = 7.6;
}
else if(amount <= 100000){
if(type == 'L')
discount = 7.5;
else if(type == 'D')
discount = 10.0;
}
else{
if(type == 'L')
discount = 10.0;
else if(type == 'D')
discount = 15.0;
}
discount = discount / 100 * amount;
double net = amount - discount;
System.out.println("Customer's
name: " + name);
System.out.println("Address:
" + address);
System.out.println("Net Amount: " +
net);
}
}
No comments:
Post a Comment