0% found this document useful (0 votes)
185 views33 pages

Java Programs for Single Dimensional Arrays

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
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)
185 views33 pages

Java Programs for Single Dimensional Arrays

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
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

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 [Link].*;
class A1
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int A[]=new int[5];

[Link]("Enter the 5 elements in an array");


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

[Link]("Array elements are : ");


for(int j=0;j<5;j++)
{
[Link](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 [Link].*;
class A2
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int A[]=new int[10];
intEsum=0,Osum=0;

[Link]("Enter the 10 elements in an Array");


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

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

[Link]("Sum of even numbers = "+Esum);


[Link]("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 [Link].*;
class A3
{
public void main()
{
Scanner sc=new Scanner([Link]);
int A[]=new int[10];

[Link]("Enter 10 elements in an Array");


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

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

[Link]("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 [Link].*;
class A4
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int A[]=new int[10];
int sum=0;
[Link]("Enter the 10 elements in an array");
for(int i=0;i<10;i++)
{
A[i]=[Link]();
}

for(int j=0;j<10;j++)
{
if(A[j]%3==0 || A[j]%5==0)
sum=sum+A[j];
}
[Link]("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 [Link].*;
class A5
{
public void main()
{
Scanner sc=new Scanner([Link]);
int A[]=new int[5];
int item,flag=0;

[Link]("Enter the 5 elements in an array");


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

[Link]("Enter the number to be searched");


item=[Link]();

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

if(flag==0)
[Link]("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 [Link].*;
class A6
{
public static void main()
{
Scanner sc=new Scanner([Link]);
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;

[Link]("Enter the country name to be searched");


country=[Link]();

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

if(flag==0)
[Link]("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 [Link].*;
classA7
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
String state[]=new String[5];
String capital[]=new String[5];
String state_name;
int flag=0;

[Link]("Enter the 5 State name with corresponding capital name");


for(int i=0;i<5;i++)
{
[Link]("Enter the State name");
state[i]=[Link]();
[Link]("Enter the capital");
capital[i]=[Link]();
}

[Link]("Enter the state name to be searched");


state_name=[Link]();

for(int j=0;j<[Link];j++)
{
if(state[j].equalsIgnoreCase(state_name))
{
flag=1;
[Link]("Search successful");
[Link](state[j]+"\t"+capital[j]);
break;
}
}
if(flag==0)
[Link]("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 [Link].*;
class A8
{
public static void main()
{
Scanner sc=new Scanner([Link]);
String CITY[]=new String[5];
long STD[]=new long[5];
String City_name;
int flag=0;

[Link]("Enter any details");


for(int i=0;i<5;i++)
{
[Link]("Enter the city name");
CITY[i]=[Link]();
[Link]("Enter the STD code");
STD[i]=[Link]();
}
[Link]("Enter the city name to be searched");
City_name=[Link]();

for(int j=0;j<5;j++)
{
if(CITY[j].equalsIgnoreCase(City_name))
{
flag=1;
[Link](CITY[j]+"\t"+STD[j]);
break;
}
}
if(flag==0)
[Link]("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 [Link].*;
class A9
{
public static void main()
{
Scanner sc=new Scanner([Link]);
String A[]=new String[5];
[Link]("Enter the 5 words in an Array");
for(int i=0;i<5;i++)
{
A[i]=[Link]();
}

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

for(int k=len-1;k>=0;k--)
{
ch=[Link](k);
Rev=Rev+ch;
}
if([Link](Rev))
[Link](n);
Rev="";
}
}
}
[Link] 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 [Link];
public class A10
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
double arr[] = new double[20];

[Link]("Enter 20 temperatures in degree Fahrenheit");


for (int i = 0; i < [Link]; i++)
{
arr[i] = [Link]();
}

[Link]("Temperatures in degree Celsius");


for (int i = 0; i < [Link]; i++)
{
double tc = 5 * ((arr[i] - 32) / 9);
[Link](tc);
}
}
}
[Link] 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 [Link];
public class A11
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int arr[] = new int[10];

[Link]("Enter 10 numbers");
for (int i = 0; i < [Link]; i++)
{
arr[i] = [Link]();
}

for (int i = 0; i < [Link]; i++)


{
if (arr[i] < 0)
[Link](arr[i] + ", ");
}

for (int i = 0; i < [Link]; i++)


{
if (arr[i] >= 0)
[Link](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 [Link];

public class A12


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

[Link]("Enter 20 numbers");
for (int i = 0; i < [Link]; i++)
{
arr[i] = [Link]();
}

[Link]("Prime Numbers:");
for (int i = 0; i < [Link]; i++)
{

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

if (c == 2)
[Link](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 [Link];

public class A13


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

String name[] = new String[n];


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

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


{
[Link]();
[Link]("Enter name of student " + (i+1) + ": ");
name[i] = [Link]();
[Link]("Enter total marks of student " + (i+1) + ": ");
totalmarks[i] = [Link]();
grandTotal += totalmarks[i];
}

double avg = grandTotal / (double)n;


[Link]("Average = " + avg);

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


{
[Link]("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 [Link];
public class A14
{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 50;
Scanner in = new Scanner([Link]);

String name[] = new String[TOTAL_STUDENTS];


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

for (int i = 0; i < [Link]; i++) {


[Link]("Enter name of student " + (i+1) + ": ");
name[i] = [Link]();
[Link]("Enter marks of student " + (i+1) + ": ");
marks[i] = [Link]();
total += marks[i];
[Link]();
}

double avg = (double)total / TOTAL_STUDENTS;


[Link]("Subject Average Marks = " + avg);
int hIdx = 0;

for (int i = 1; i < [Link]; i++) {


if (marks[i] > marks[hIdx])
hIdx = i;
}
[Link]("Highest Marks = " + marks[hIdx]);
[Link]("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 [Link];

public class A15


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

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


{
[Link]("Enter student " + (i+1) + " details:");
[Link]("Roll No: ");
rollNo[i] = [Link]();
[Link]();
[Link]("Name: ");
name[i] = [Link]();
[Link]("Subject 1 Marks: ");
s1[i] = [Link]();
[Link]("Subject 2 Marks: ");
s2[i] = [Link]();
[Link]("Subject 3 Marks: ");
s3[i] = [Link]();
[Link]("Subject 4 Marks: ");
s4[i] = [Link]();
[Link]("Subject 5 Marks: ");
s5[i] = [Link]();
[Link]("Subject 6 Marks: ");
s6[i] = [Link]();

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';
}

[Link]();

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


{
[Link](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 [Link];

public class A16


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

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


[Link]("Enter student " + (i+1) + " details:");
[Link]("Marks in English: ");
english[i] = [Link]();
[Link]("Marks in Maths: ");
maths[i] = [Link]();
[Link]("Marks in Science: ");
science[i] = [Link]();
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++) {


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

[Link]("Class Average in English = " + ((double)engTotal /


TOTAL_STUDENTS));
[Link]("Class Average in Maths = " + ((double)mathsTotal /
TOTAL_STUDENTS));
[Link]("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 [Link];

public class A17


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

final int NUM_COUNT = 20;


Scanner in = new Scanner([Link]);
int i = 0;

int arr[] = new int[NUM_COUNT];


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

[Link]("Enter 20 numbers:");
for (i = 0; i < NUM_COUNT; i++) {
arr[i] = [Link]();
}

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];
}

[Link]("Even Numbers:");
for (i = 0; i < eIdx; i++) {
[Link](even[i] + " ");
}

[Link]("\nOdd Numbers:");
for (i = 0; i < oIdx; i++) {
[Link](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 [Link];

public class A18


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

[Link]("Enter 20 numbers");
for (int i = 0; i < [Link]; i++) {
arr[i] = [Link]();
}

[Link]("Perfect Squares are:");


for (int i = 0; i < [Link]; i++) {
double sr = [Link](arr[i]);
if ((sr - [Link](sr)) == 0)
[Link](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 [Link];

public class A19


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

[Link]("Enter 10 decimal numbers");


for (int i = 0; i < [Link]; i++) {
a[i] = [Link]();
b[i] = (int)a[i];
}

[Link]("Truncated numbers");
for (int i = 0; i < [Link]; i++) {
[Link](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 [Link];

public class A20


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

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


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

[Link]("\nStudents with Average above 80:");


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

[Link]("\nStudents with Average below 40:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] < 40)
[Link](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 [Link];

public class A21


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

Scanner in = new Scanner([Link]);

int P[] = new int[6];


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

[Link]("Enter 6 elements of array P:");


for (i = 0; i < [Link]; i++) {
P[i] = [Link]();
}

[Link]("Enter 4 elements of array Q:");


for (i = 0; i < [Link]; i++) {
Q[i] = [Link]();
}

i = 0;
while(i < [Link]) {
R[i] = P[i];
i++;
}

int j = 0;
while(j < [Link]) {
R[i++] = Q[j++];
}

[Link]("Elements of Array R:");


for (i = 0; i < [Link]; i++) {
[Link](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 [Link];

public class A22


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

[Link]("Enter graduation year to search: ");


int year = [Link]();

int l = 0, h = [Link] - 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)
[Link]("Record does not exist");
else
[Link]("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 [Link];

public class KboatAvgMarks


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

Scanner in = new Scanner([Link]);


[Link]("Enter number of students: ");
int n = [Link]();

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


[Link]("Enter student " + (i+1) + " details:");
[Link]("Roll No: ");
rollNo[i] = [Link]();
[Link]();
[Link]("Name: ");
name[i] = [Link]();
[Link]("Subject 1 Marks: ");
s1[i] = [Link]();
[Link]("Subject 2 Marks: ");
s2[i] = [Link]();
[Link]("Subject 3 Marks: ");
s3[i] = [Link]();
avg[i] = (s1[i] + s2[i] + s3[i]) / 3.0;
}

[Link]("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";
[Link](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 [Link];

public class A24


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

Scanner in = new Scanner([Link]);


int arr[] = new int[10];

[Link]("Enter 10 integers:");
for (int i = 0; i < 10; i++) {
arr[i] = [Link]();
}

[Link]("Integers in reverse order:");


for (int i = 9; i >= 0; i--) {
[Link](arr[i] + " ");
}
}
}

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

public class A25


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

Scanner in = new Scanner([Link]);


[Link]("Enter a number: ");
long num = [Link]();
int dCount[] = new int[10];

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

[Link]("Digit\tOccurence");
for (int i = 0; i < 10; i++) {
if (dCount[i] != 0) {
[Link](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 [Link];

public class A26


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

Scanner in = new Scanner([Link]);


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

[Link]("Enter number to search: ");


int n = [Link]();

int l = 0, h = [Link] - 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) {
[Link]("Search element not found");
}
else {
[Link](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 [Link];

public class A27


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

[Link]("Enter Feb daily temperatures:");


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

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;

[Link]("Hottest day = " + (high + 1));


[Link]("Coldest day = " + (low + 1));
[Link]("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.

[Link] 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.

Common questions

Powered by AI

Create arrays to store roll numbers, names, and marks for each subject. Calculate each candidate's total marks and compute their percentage by dividing total marks by the maximum possible marks. Assign grades based on the percentage: A for 80-100%, B for 60-79%, C for 40-59%, and D for below 40%. Use conditional statements to determine and assign grades during the calculation process .

The program involves creating an array to store the numbers and two variables, Esum and Osum, to store the sums of even and odd numbers respectively. You loop through each element of the array; if the number is even (checked using modulus operation A[j]%2==0), add it to Esum. If odd, add it to Osum. After processing all numbers, print the results stored in Esum and Osum. The logical constructs used include loops, conditional statements, and arithmetic operations .

Store the words in an array of strings. For each word, create a reverse by iterating backwards through the characters of the word and appending to a new string. Use equalsIgnoreCase to compare the word with its reverse to check for palindromes. Print words that match this criteria. This approach requires string manipulation and comparison .

To implement this program, you need to follow these steps: First, import the java.util.Scanner class for reading input. Create an array 'A' of size 10 to store the integers. Initialize the Scanner class and prompt the user to input 10 integers using a loop. Store each integer input in the array. Finally, iterate over the array to print out each element. The key steps are input processing, storage in array using indexing, and iteration for displaying outputs .

Binary Search requires working on a sorted array. Begin by setting pointers for the low, high, and mid positions. Compare the middle element with the target year; if it matches, output ‘Record exists’. If the target is lower, narrow the search to the left subarray by adjusting the high pointer. If higher, adjust the low pointer for the right subarray. Repeat until the year is found or the subarray is exhausted, resulting in 'Record does not exist' if not found .

To find the greatest integer, initialize a variable 'Great' with the value of the first element in the array. Traverse the array using a loop, comparing each element with 'Great'. If an element is greater than 'Great', update 'Great' with this element's value. After the loop completes, 'Great' holds the value of the greatest number in the array .

Implement Bubble Sort by repeatedly stepping through the list of city names, comparing each pair of adjacent items and swapping them if they are in the wrong order. Continue this process, each time moving the next-largest item to its correct position, until the list is sorted. This method is simple but may be inefficient for large datasets due to its quadratic time complexity .

Implement an array to store names and another to store the marks. Use a loop to input student names and marks, and calculate the total marks. Maintain a variable like 'hIdx' to track the index of the highest marks; update it whenever a higher score is found during iteration. After iterating through the array, display the name and marks of the student at 'hIdx' .

Create two arrays: one for the seven wonders and another for their corresponding countries. Prompt user input for a country name, iterate through the country array, and check if any entry matches the user-input using equalsIgnoreCase for case insensitivity. If a match is found, print the wonder indexed at the same position in the wonder array. If not found, output ‘Sorry Not Found!’ This involves using conditionals for matching and string manipulation for outputs .

Use the formula: Celsius = (Fahrenheit - 32) * 5/9. Create an array to store temperatures in Fahrenheit. Loop through the array, apply the conversion formula to each temperature, and store or display the converted temperatures. This straightforward temperature conversion involves basic arithmetic operations .

You might also like