0% found this document useful (0 votes)
157 views

Array SDA

A document containing 10 programming problems involving arrays in Java. Each problem is presented with sample code to input and process array data, such as storing integers or strings in arrays, searching arrays, and performing calculations on array elements.

Uploaded by

Piyush Sarswat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views

Array SDA

A document containing 10 programming problems involving arrays in Java. Each problem is presented with sample code to input and process array data, such as storing integers or strings in arrays, searching arrays, and performing calculations on array elements.

Uploaded by

Piyush Sarswat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

PROGRAMMERS POINT

Computer Application / Computer Science (For ICSE/ISC/CBSE Board)


Add: Behind Kundkund Dharmshala, Opp. Collectorate Office, Agra
Shop No. 11-12, Block No.19, Ist Floor, Cloth Market, Near McDonald, sanjay Place, Agra
865/C, Sector- 6, Avas vikas Colony, Sikandra, Agra Mob: 8273651685,
7017954184

Class : X (SEM-2) ARRAY

1. WAP in java to input 10 integer elements in an array and print them.

import java.util.*;
class A1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int A[]=new int[5];

System.out.println("Enter the 5 elements in an array");


for(int i=0;i<5;i++)
{
A[i]=sc.nextInt();
}

System.out.println("Array elements are : ");


for(int j=0;j<5;j++)
{
System.out.print(A[j]+"\t");
}
}
}
2. WAP in Java to store 20 numbers (Including Even and Odd numbers) in a S.D.A and display
the sum of all Even numbers and all Odd numbers separately stored in the cell.

import java.util.*;
class A2
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int A[]=new int[10];
intEsum=0,Osum=0;

System.out.println("Enter the 10 elements in an Array");


for(int i=0;i<10;i++)
{
A[i]=sc.nextInt();
}

for(int j=0;j<10;j++)
{
if(A[j]%2==0)
Esum=Esum+A[j];
else
Osum=Osum+A[j];
}

System.out.println("Sum of even numbers = "+Esum);


System.out.println("Sum of Odd numbers = "+Osum);
}
}
3. WAP to accept 10 different numbers in a Single Dimensional Array(SDA). Display the
greatest element of the array elements.

import java.util.*;
class A3
{
public void main()
{
Scanner sc=new Scanner(System.in);
int A[]=new int[10];

System.out.println("Enter 10 elements in an Array");


for(int i=0;i<10;i++)
{
A[i]=sc.nextInt();
}

int Great=A[0];
for(int j=0;j<10;j++)
{
if(A[j]>Great)
Great=A[j];
}

System.out.println("Greatest number in an array = "+Great);


}
}
4. WAP to accept 10 different numbers in a Single Dimensional Array(SDA). Display the sum
of all the numbers which are divisible by either 3 or 5.
import java.util.*;
class A4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int A[]=new int[10];
int sum=0;
System.out.println("Enter the 10 elements in an array");
for(int i=0;i<10;i++)
{
A[i]=sc.nextInt();
}

for(int j=0;j<10;j++)
{
if(A[j]%3==0 || A[j]%5==0)
sum=sum+A[j];
}
System.out.println("Sum = "+sum);
}
}
5. WAP to accept 10 different numbers in a Single Dimensional Array(SDA). Now, enter a
number and search whether the number is present or not in the list of array elements by using
the ‘Linear Search’ technique.

import java.util.*;
class A5
{
public void main()
{
Scanner sc=new Scanner(System.in);
int A[]=new int[5];
int item,flag=0;

System.out.println("Enter the 5 elements in an array");


for(int i=0;i<5;i++)
{
A[i]=sc.nextInt();
}

System.out.println("Enter the number to be searched");


item=sc.nextInt();

for(int j=0;j<5;j++)
{
if(A[j]==item)
{
flag=1;
System.out.println("Number is present in the list");
break;
}
}

if(flag==0)
System.out.println("Number is not present in the list");
}
}
6. WAP 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
country along with its Wonder.

Seven Wonders:Chichen Itza, Christ the Redeemer, TajMahal, Great Wall of China, Machu
Picchu, Petra, Colosseum.
Location: Maxico, Brazil, India, China, Peru, Jordan, Italy

Example: Input: Country Name India


Output: India TajMahal
Country Name USA
Output: ‘Sorry Not Found!’

import java.util.*;
class A6
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String WON[]={"Chichen Itza", "Christ the Redeemer", "TajMahal", "Great Wall of
China", "Machu Picchu", "Petra", "Colosseum"};

String LOC[]={"Maxico", "Brazil", "India", "China", "Peru", "Jordan", "Italy"};


String country;
int flag=0;

System.out.println("Enter the country name to be searched");


country=sc.next();

for(int j=0;j<LOC.length;j++)
{
if(LOC[j].equalsIgnoreCase(country))
{
flag=1;
System.out.println(WON[j]+"\t"+LOC[j]);
break;
}
}

if(flag==0)
System.out.println("Sorry! Not found");
}
}
7. WAP to accept 10 states and 10 capitals of a country in two different Single Dimensional
Array(SDA). Now, enter a state of the country to display it’s capital. If it is present then display
it’s capital otherwise, display a relevant message.

Sample Input: Enter the state and the capital


Bihar, Patna, West Bengal, Kolkata and so on…………
Sample Output: Enter the state whose capital is to be searched:
West Bengal The capital is Kolkata

import java.util.*;
classA7
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String state[]=new String[5];
String capital[]=new String[5];
String state_name;
int flag=0;

System.out.println("Enter the 5 State name with corresponding capital name");


for(int i=0;i<5;i++)
{
System.out.println("Enter the State name");
state[i]=sc.next();
System.out.println("Enter the capital");
capital[i]=sc.next();
}

System.out.println("Enter the state name to be searched");


state_name=sc.next();

for(int j=0;j<state.length;j++)
{
if(state[j].equalsIgnoreCase(state_name))
{
flag=1;
System.out.println("Search successful");
System.out.println(state[j]+"\t"+capital[j]);
break;
}
}
if(flag==0)
System.out.println("State name not in the list");
}
}
8. WAP to accept the names of 10 cities in a single dimensional string array and their
STD(Subscribers Trunk Dialing) codes in another single dimensional integer array. Search for
the name of a city input by the user in the list. If found, display the message “Search
unsuccessful, no such city in the list”.

import java.util.*;
class A8
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String CITY[]=new String[5];
long STD[]=new long[5];
String City_name;
int flag=0;

System.out.println("Enter any details");


for(int i=0;i<5;i++)
{
System.out.println("Enter the city name");
CITY[i]=sc.nextLine();
System.out.println("Enter the STD code");
STD[i]=sc.nextLong();
}
System.out.println("Enter the city name to be searched");
City_name=sc.next();

for(int j=0;j<5;j++)
{
if(CITY[j].equalsIgnoreCase(City_name))
{
flag=1;
System.out.println(CITY[j]+"\t"+STD[j]);
break;
}
}
if(flag==0)
System.out.println("Seacrch unsuccessful");
}}
9: WAP to stores 10 words in a Single Dimensional Array. Display only those words which are
Palindromes.
Sample Input: MADAM, TEACHER, SCHOOL, NITIN
Sample Output: MADAM
NITIN
import java.util.*;
class A9
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String A[]=new String[5];
System.out.println("Enter the 5 words in an Array");
for(int i=0;i<5;i++)
{
A[i]=sc.next();
}

for(int j=0;j<5;j++)
{
String n=A[j];
String Rev="";
char ch;
int len=n.length();

for(int k=len-1;k>=0;k--)
{
ch=n.charAt(k);
Rev=Rev+ch;
}
if(n.equalsIgnoreCase(Rev))
System.out.println(n);
Rev="";
}
}
}
10.Write a program in Java to store 20 temperatures in °F in a Single Dimensional Array
(SDA) and display all the temperatures after converting them into °C.
Hint: (c/5) = (f - 32) / 9

import java.util.Scanner;
public class A10
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
double arr[] = new double[20];

System.out.println("Enter 20 temperatures in degree Fahrenheit");


for (int i = 0; i < arr.length; i++)
{
arr[i] = in.nextDouble();
}

System.out.println("Temperatures in degree Celsius");


for (int i = 0; i < arr.length; i++)
{
double tc = 5 * ((arr[i] - 32) / 9);
System.out.println(tc);
}
}
}
11.Write a program in Java to store 10 numbers (including positive and negative numbers) in a
Single Dimensional Array (SDA). Display all the negative numbers followed by the positive
numbers without changing the order of the numbers.
Sample Input:

n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]

15 21 -32 -41 54 61 71 -19 -44 52

Sample Output: -32, -41, -19, -44, 15, 21, 54, 61, 71, 52
import java.util.Scanner;
public class A11
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int arr[] = new int[10];

System.out.println("Enter 10 numbers");
for (int i = 0; i < arr.length; i++)
{
arr[i] = in.nextInt();
}

for (int i = 0; i < arr.length; i++)


{
if (arr[i] < 0)
System.out.print(arr[i] + ", ");
}

for (int i = 0; i < arr.length; i++)


{
if (arr[i] >= 0)
System.out.print(arr[i] + ", ");
}
}
}
12. 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
.

Sample Output: 71, 67, 19, 31


import java.util.Scanner;

public class A12


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

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++)
{

int c = 0;
for (int j = 1; j <= arr[i]; j++)
{
if (arr[i] % j == 0)
{
c++;
}
}

if (c == 2)
System.out.print(arr[i] + ", ");
}
}}
13. 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;

public class A13


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();

String name[] = new String[n];


int totalmarks[] = new int[n];
int grandTotal = 0;

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


{
in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) + ": ");
totalmarks[i] = in.nextInt();
grandTotal += totalmarks[i];
}

double avg = grandTotal / (double)n;


System.out.println("Average = " + avg);

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


{
System.out.println("Deviation for " + name[i] + " = "
+ (totalmarks[i] - avg));
}
}
}
14. The marks obtained by 50 students in a subject are tabulated as follows:-

Name Marks

..... .....

..... .....

Write a program to input the names and marks of the students in the subject.
Calculate and display:
(a) The subject average marks (subject average marks = subject total/50).
(b) The highest marks in the subject and the name of the student. (The maximum marks in the
subject are 100.)
import java.util.Scanner;
public class A14
{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 50;
Scanner in = new Scanner(System.in);

String name[] = new String[TOTAL_STUDENTS];


int marks[] = new int[TOTAL_STUDENTS];
int total = 0;

for (int i = 0; i < name.length; i++) {


System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter marks of student " + (i+1) + ": ");
marks[i] = in.nextInt();
total += marks[i];
in.nextLine();
}

double avg = (double)total / TOTAL_STUDENTS;


System.out.println("Subject Average Marks = " + avg);
int hIdx = 0;

for (int i = 1; i < marks.length; i++) {


if (marks[i] > marks[hIdx])
hIdx = i;
}
System.out.println("Highest Marks = " + marks[hIdx]);
System.out.println("Name = " + name[hIdx]);
}
}
15. Write a program in Java using arrays:
(a) To store the Roll No., Name and marks in 3 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:

Percentage Marks Grade

From 80 to 100 A

From 60 to 79 B

From 40 to 59 C

Less than 40 D

import java.util.Scanner;

public class A15


{
public static void main(String args[])
{
final int TOTAL_STUDENTS = 100;
Scanner in = new Scanner(System.in);

int rollNo[] = new int[TOTAL_STUDENTS];


String name[] = new String[TOTAL_STUDENTS];
int s1[] = new int[TOTAL_STUDENTS];
int s2[] = new int[TOTAL_STUDENTS];
int s3[] = new int[TOTAL_STUDENTS];
int s4[] = new int[TOTAL_STUDENTS];
int s5[] = new int[TOTAL_STUDENTS];
int s6[] = new int[TOTAL_STUDENTS];
double p[] = new double[TOTAL_STUDENTS];
char g[] = new char[TOTAL_STUDENTS];

for (int i = 0; i < TOTAL_STUDENTS; i++)


{
System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
System.out.print("Subject 4 Marks: ");
s4[i] = in.nextInt();
System.out.print("Subject 5 Marks: ");
s5[i] = in.nextInt();
System.out.print("Subject 6 Marks: ");
s6[i] = in.nextInt();

p[i] = (((s1[i] + s2[i] + s3[i] + s4[i]


+ s5[i] + s6[i]) / 600.0) * 100);

if (p[i] < 40)


g[i] = 'D';
else if (p[i] < 60)
g[i] = 'C';
else if (p[i] < 80)
g[i] = 'B';
else
g[i] = 'A';
}

System.out.println();

for (int i = 0; i < TOTAL_STUDENTS; i++)


{
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ p[i] + "\t"
+ g[i]);
}
}
}
Question 16.
The class teacher wants to store the marks obtained in English, Maths and Science of her class
having 40 students. Write a program to input marks in Eng, Science and Maths by using three
single dimensional arrays. Calculate and print the following information:
(i) Average marks secured by each student.
(ii) Class average in each subject.
[Hint: Class average is the average marks obtained by 40 students in a particular subject.]
import java.util.Scanner;

public class A16


{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 40;
Scanner in = new Scanner(System.in);

int english[] = new int[TOTAL_STUDENTS];


int maths[] = new int[TOTAL_STUDENTS];
int science[] = new int[TOTAL_STUDENTS];
double avgMarks[] = new double[TOTAL_STUDENTS];

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


System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Marks in English: ");
english[i] = in.nextInt();
System.out.print("Marks in Maths: ");
maths[i] = in.nextInt();
System.out.print("Marks in Science: ");
science[i] = in.nextInt();
avgMarks[i] = (english[i] + maths[i] + science[i]) / 3.0;
}

int engTotal = 0, mathsTotal = 0, sciTotal = 0;

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


System.out.println("Average marks of student " + (i+1) + " = " + avgMarks[i]);
engTotal += english[i];
mathsTotal += maths[i];
sciTotal += science[i];
}

System.out.println("Class Average in English = " + ((double)engTotal /


TOTAL_STUDENTS));
System.out.println("Class Average in Maths = " + ((double)mathsTotal /
TOTAL_STUDENTS));
System.out.println("Class Average in Science = " + ((double)sciTotal /
TOTAL_STUDENTS));
}
}
Question 17.
Write a program in Java to accept 20 numbers in a single dimensional array arr[20]. Transfer and
store all the even numbers in an array even[ ] and all the odd numbers in another array odd[ ].
Finally, print the elements of both the arrays.
import java.util.Scanner;

public class A17


{
public static void main(String args[]) {

final int NUM_COUNT = 20;


Scanner in = new Scanner(System.in);
int i = 0;

int arr[] = new int[NUM_COUNT];


int even[] = new int[NUM_COUNT];
int odd[] = new int[NUM_COUNT];

System.out.println("Enter 20 numbers:");
for (i = 0; i < NUM_COUNT; i++) {
arr[i] = in.nextInt();
}

int eIdx = 0, oIdx = 0;


for (i = 0; i < NUM_COUNT; i++) {
if (arr[i] % 2 == 0)
even[eIdx++] = arr[i];
else
odd[oIdx++] = arr[i];
}

System.out.println("Even Numbers:");
for (i = 0; i < eIdx; i++) {
System.out.print(even[i] + " ");
}

System.out.println("\nOdd Numbers:");
for (i = 0; i < oIdx; i++) {
System.out.print(odd[i] + " ");
}
}}
Question 18
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
.

Sample Output: 49, 64, 81


import java.util.Scanner;

public class A18


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

System.out.println("Perfect Squares are:");


for (int i = 0; i < arr.length; i++) {
double sr = Math.sqrt(arr[i]);
if ((sr - Math.floor(sr)) == 0)
System.out.print(arr[i] + ", ");
}
}
}
Question 19.
Write a program to accept 10 different decimal numbers (double data type) in a Single Dimensional
Array (say, A). Truncate the fractional part of each number of the array A and store their integer
part in another array (say, B).
import java.util.Scanner;

public class A19


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double a[] = new double[10];
int b[] = new int[10];

System.out.println("Enter 10 decimal numbers");


for (int i = 0; i < a.length; i++) {
a[i] = in.nextDouble();
b[i] = (int)a[i];
}

System.out.println("Truncated numbers");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + ", ");
}
}
}
Question 20.
The annual examination result of 50 students in a class is tabulated in a Single Dimensional Array
(SDA) is as follows:

Roll No. Subject A Subject B Subject C

....... ....... ....... .......

....... ....... ....... .......

....... ....... ....... .......

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;

public class A20


{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 50;
Scanner in = new Scanner(System.in);

int rollNo[] = new int[TOTAL_STUDENTS];


int sA[] = new int[TOTAL_STUDENTS];
int sB[] = new int[TOTAL_STUDENTS];
int sC[] = new int[TOTAL_STUDENTS];
double avg[] = new double[TOTAL_STUDENTS];

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


System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
System.out.print("Subject A Marks: ");
sA[i] = in.nextInt();
System.out.print("Subject B Marks: ");
sB[i] = in.nextInt();
System.out.print("Subject C Marks: ");
sC[i] = in.nextInt();
avg[i] = (sA[i] + sB[i] + sC[i]) / 3.0;
}
System.out.println("\nRoll No\tAverage Marks");
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average above 80:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] > 80)
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average below 40:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] < 40)
System.out.println(rollNo[i] + "\t" + avg[i]);
}
}}

Question 21.
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.

Input Input Output

P[ ] Q[ ] R[ ]

4 19 4

6 23 6

1 7 1

2 8 2

3 3

10 10

19

23

import java.util.Scanner;

public class A21


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

int P[] = new int[6];


int Q[] = new int[4];
int R[] = new int[10];
int i = 0;

System.out.println("Enter 6 elements of array P:");


for (i = 0; i < P.length; i++) {
P[i] = in.nextInt();
}

System.out.println("Enter 4 elements of array Q:");


for (i = 0; i < Q.length; i++) {
Q[i] = in.nextInt();
}

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] + " ");
}
}
}

Question 22.
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".
Sample Input:

n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]

198 198 199 199 199 200 200 200 200 201
2 7 3 6 9 3 6 7 9 0

import java.util.Scanner;

public class A22


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};

System.out.print("Enter graduation year to search: ");


int year = in.nextInt();

int l = 0, h = n.length - 1, idx = -1;


while (l <= h) {
int m = (l + h) / 2;
if (n[m] == year) {
idx = m;
break;
}
else if (n[m] < year) {
l = m + 1;
}
else {
h = m - 1;
}
}

if (idx == -1)
System.out.println("Record does not exist");
else
System.out.println("Record exists");
}}
Question 23.
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 Remark

85 — 100 Excellent

75 — 84 Distinction

60 — 74 First Class

40 — 59 Pass

Less than 40 Poor

import java.util.Scanner;

public class KboatAvgMarks


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter number of students: ");
int n = in.nextInt();

int rollNo[] = new int[n];


String name[] = new String[n];
int s1[] = new int[n];
int s2[] = new int[n];
int s3[] = new int[n];
double avg[] = new double[n];

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


System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
avg[i] = (s1[i] + s2[i] + s3[i]) / 3.0;
}

System.out.println("Roll No\tName\tRemark");
for (int i = 0; i < n; i++) {
String remark;
if (avg[i] < 40)
remark = "Poor";
else if (avg[i] < 60)
remark = "Pass";
else if (avg[i] < 75)
remark = "First Class";
else if (avg[i] < 85)
remark = "Distinction";
else
remark = "Excellent";
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ remark);
}
}
}

Question 24.
Write a program that reads ten integers and displays them in the reverse order in which they were
read.
Answer
import java.util.Scanner;

public class A24


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


int arr[] = new int[10];

System.out.println("Enter 10 integers:");
for (int i = 0; i < 10; i++) {
arr[i] = in.nextInt();
}

System.out.println("Integers in reverse order:");


for (int i = 9; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}

Question 25.
Write a program that reads a long number, counts and displays the occurrences of each digit in it.
Answer
import java.util.Scanner;

public class A25


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter a number: ");
long num = in.nextLong();
int dCount[] = new int[10];

while (num != 0) {
int d = (int)(num % 10);
dCount[d] = dCount[d] + 1;
num /= 10;
}

System.out.println("Digit\tOccurence");
for (int i = 0; i < 10; i++) {
if (dCount[i] != 0) {
System.out.println(i + "\t" + dCount[i]);
}
}
}
}

Question 26.
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
Answer
import java.util.Scanner;

public class A26


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


int arr[] = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};

System.out.print("Enter number to search: ");


int n = in.nextInt();

int l = 0, h = arr.length - 1, index = -1;


while (l <= h) {
int m = (l + h) / 2;
if (arr[m] < n)
l = m + 1;
else if (arr[m] > n)
h = m - 1;
else {
index = m;
break;
}

if (index == -1) {
System.out.println("Search element not found");
}
else {
System.out.println(n + " found at position " + index);
}
}}
Question 27
Declare a single dimensional array of size 28 to store daily temperatures for the month of February.
Using this structure, write a program to find:
1. The hottest day of the month
2. The coldest day of the month
3. The average temperature of the month

Answer
import java.util.Scanner;

public class A27


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double febTemp[] = new double[28];
int n = febTemp.length;

System.out.println("Enter Feb daily temperatures:");


for (int i = 0; i < n; i++) {
febTemp[i] = in.nextDouble();
}

double sum = 0.0;


int low = 0, high = 0;
for (int i = 0; i < n; i++) {
if (febTemp[i] < febTemp[low])
low = i;

if (febTemp[i] > febTemp[high])


high = i;

sum += febTemp[i];
}

double avg = sum / n;

System.out.println("Hottest day = " + (high + 1));


System.out.println("Coldest day = " + (low + 1));
System.out.println("Average Temperature = " + avg);
}
}
28. WAP to input and store the weight of ten people, Sort and display them in ascending order using the
‘Bubble sort’ technique.

29.WAP to initialize 5 different city names in a Single Dimensional Array. Arrange the names in
ascending order by using the ‘Bubble Sort’ technique and display them.
Input: Delhi, Bangalore, Agra, Mumbai, Calcutta.
Output: Agra, Bangalore, Calcutta, Delhi, Mumbai.

30. WAP in Java to stores the runs scored by 11 Indian Cricket Players in an innings along with their
names. Now display the name of the cricketer who has made the highest score in that innings along
with the runs.

31. Design a class to accept and store 10 strings into an array and print the string starting with vowel.

32. Write a program to accept 10 characters into an array and perform the following operations.
a. Count the uppercase letters
b. Count the digits
c. Display the vowels present in the array

33. Define a class accept and store 10 strings into the array and print the strings with even number of characters.

34. Write a program to store n real numbers in an array A, Shift the integer part in another array B and shifting
fractional part in array C. Print minimum integer from Array B and maximum fractional number from
array C along with index numbers.

35. Write a program to store a single dimensional array, count and print only those name and length which
are starting and ending with vowel.

You might also like