Java - Util.scanner: Import Public Class Public Void New - Int New
Java - Util.scanner: Import Public Class Public Void New - Int New
Java - Util.scanner: Import Public Class Public Void New - Int New
n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
Sample Output: -32, -41, -19, 44, 15, 21, 54, 61, 71, 52
import java.util.Scanner;
System.out.println("Enter 10 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
Output
Question 4
Write a program in Java to store 20 numbers in a Single Dimensional Array (SDA).
Display the numbers which are prime.
Sample Input:
n[0] n[1] n[2] n[3] n[4] n[5] ... n[16] n[17] n[18] n[19]
45 65 77 71 90 67 ... 82 19 31 52
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 1)
continue;
if (isPrime)
System.out.print(arr[i] + ", ");
}
}
}
Output
Question 5
Write a program to accept name and total marks of N number of students in two
single subscript arrays 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;
Output
Question 7
Write a program in Java using arrays:
(a) To store the Roll No., Name and marks in six subjects for 100 students.
(b) Calculate the percentage of marks obtained by each candidate. The maximum
marks in each subject are 100.
(c) Calculate the Grade as per the given criteria:
From 80 to 100 A
From 60 to 79 B
From 40 to 59 C
Less than 40 D
import java.util.Scanner;
System.out.println();
Output
Question 8
Write a program to accept a list of 20 integers. Sort the first 10 numbers in
ascending order and next the 10 numbers in descending order by using 'Bubble
Sort' technique. Finally, print the complete list of integers.
import java.util.Scanner;
Output
Question 11
Write a program to store 20 numbers in a Single Dimensional Array (SDA). Now,
display only those numbers that are perfect squares.
n[0] n[1] n[2] n[3] n[4] n[5] ... n[16] n[17] n[18] n[19]
12 45 49 78 64 77 ... 81 99 45 33
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
Output
Question 14
The annual examination result of 50 students in a class is tabulated in a Single
Dimensional Array (SDA) is as follows:
Write a program to read the data, calculate and display the following:
(a) Average marks obtained by each student.
(b) Print the roll number and the average marks of the students whose average is
above. 80.
(c) Print the roll number and the average marks of the students whose average is
below 40.
import java.util.Scanner;
Question 15
Write a program to store 6 elements in an array P and 4 elements in an array Q.
Now, produce a third array R, containing all the elements of array P and Q. Display
the resultant array.
P[ ] Q[ ] R[ ]
4 19 4
6 23 6
1 7 1
2 8 2
3 3
10 10
19
Input Input Output
23
7
8
import java.util.Scanner;
i = 0;
while(i < P.length) {
R[i] = P[i];
i++;
}
int j = 0;
while(j < Q.length) {
R[i++] = Q[j++];
}
System.out.println("Elements of Array R:");
for (i = 0; i < R.length; i++) {
System.out.print(R[i] + " ");
}
}
}
Output
4 6
Question 2
int a[] ={2,4,6,8,10};
a[0]=23;
a[3]=a[1];
int c= a[0]+a[1];
System.out.println("Sum = "+c);
Output
Sum = 27
Question 3
int a[]=new int [5];
a[0]=4; a[1]=8; a[2]=7; a[3]=12; a[4]=3;
System.out.println(a[2+1]);
Output
12
Question 4
int a[4]={2,4,6,8};
for(i=0;i<=1;i++)
{
s=a[i]+a[3-i];
System.out.println(s);
}
Output
10
10
Question 2
Question 3
Variables are useful for keeping track of a single piece of information but as we collect
more and more information, keeping the variables organized can be complicated. In such
situations, we need arrays to solve the problems in a much better and efficient way.
Question 4
The data type that represents a number of similar or different data under single
declaration is called as composite data type. An array is a group or a collection of same
type of variables. Hence, Array is a composite data type.
Question 5
Double Dimensional Array contains multiple rows and multiple columns. The syntax of
declaring a Double Dimensional Array is:
<type> <array-variable>[][] = new <type>[<rows>][<columns>];
OR
<type> [][] <array-variable> = new <type>[<rows>][<columns>];
Differentiate between the following
Question 1
Subscript is the index of the element in the array whereas Subscripted variable is the name
of the array when it is used with a subscript to access a single element of the array.
Question 2
char a[5] is an array of char data type that can hold 5 characters whereas int a[5] is an
array of int data type that can hold 5 integer values.
Question 3
An ordinary variable can hold only one value whereas an array variable can refer to a
group of values of the same data type by using a subscript.
Question 4
Sorting Searching
Sorting means to arrange the elements of the Searching means to search for a term or
array in ascending or descending order. value in an array.
Bubble sort and Selection sort are examples of Linear search and Binary search are
sorting techniques. examples of search techniques.
Question 5
Linear search works on sorted and unsorted Binary search works on only sorted arrays
arrays (ascending or descending)
Each element of the array is checked Array is successively divided into 2 halves and
against the target value until the element is the target element is searched either in the first
found or end of the array is reached half or in the second half
Question 6
Selection Sort selects the smallest element from Bubble Sort compares adjacent
unsorted sub-array and swaps it with the leftmost elements and swaps them if they are in
unsorted element. wrong order.
Performs lesser number of swaps to sort the same Performs more number of swaps to
array relative to Bubble Sort sort the array
length length()
It gives the length of an array i.e. the number of It gives the number of characters
elements stored in an array. present in a string