0% found this document useful (0 votes)
96 views13 pages

Class X Arrays Programs

The document contains multiple Java programs that demonstrate various operations on single-dimensional arrays, including calculating sums of even and odd numbers, sorting, finding prime numbers, and calculating averages and deviations of student marks. Each program is structured to take user input, perform specific calculations or manipulations, and display the results. The examples cover a range of topics such as sorting techniques, mathematical operations, and data organization.

Uploaded by

raj_bhure
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)
96 views13 pages

Class X Arrays Programs

The document contains multiple Java programs that demonstrate various operations on single-dimensional arrays, including calculating sums of even and odd numbers, sorting, finding prime numbers, and calculating averages and deviations of student marks. Each program is structured to take user input, perform specific calculations or manipulations, and display the results. The examples cover a range of topics such as sorting techniques, mathematical operations, and data organization.

Uploaded by

raj_bhure
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

ARRAYS:

Write a program in Java to store 20 numbers (even and odd numbers) in a


Single Dimensional Array (SDA). Calculate and display the sum of all even
numbers and all odd numbers separately.
import [Link];
public class KboatSDAOddEvenSum{
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]();
}

int oddSum = 0, evenSum = 0;

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


if (arr[i] % 2 == 0)
evenSum += arr[i];
else
oddSum += arr[i];
}

[Link]("Sum of Odd numbers = " + oddSum);


[Link]("Sum of Even numbers = " + evenSum);
}}

Define a class to declare an array of size twenty of double datatype, accept the
elements into the array and perform the following :
Calculate and print the product of all the elements.
Print the square of each element of the array.
import [Link];
public class KboatSDADouble{
public static void main(String[] args)
{
Scanner in = new Scanner([Link]);
double arr[] = new double[20];
int l = [Link];
double p = 1.0;

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

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


{
p *= arr[i];
}
[Link]("Product = " + p);
[Link]("Square of array elements :");
for (int i = 0; i < l; i++)
{
double sq = [Link](arr[i], 2);
[Link](sq + " ");
}
}}

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 [Link];
public class KboatSDANumbers{
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] + ", ");
}
}}

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

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 KboatSDAMarks{
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));
}
}}

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:

Percentage
Grade
Marks

From 80 to 100 A

From 60 to 79 B

From 40 to 59 C

Less than 40 D

import [Link];
public class KboatSDAGrades{
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]);
}
}}

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 [Link];
public class KboatSDABubbleSort{
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]();
}

//Sort first half in ascending order


for (int i = 0; i < [Link] / 2 - 1; i++) {
for (int j = 0; j < [Link] / 2 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Sort second half in descending order


for (int i = 0; i < [Link] / 2 - 1; i++) {
for (int j = [Link] / 2; j < [Link] - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Print the final sorted array


[Link]("\nSorted Array:");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}}

Define a class pin code and store the given pin codes in a single dimensional
array. Sort these pin codes in ascending order using the Selection Sort technique
only. Display the sorted array.
110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033
public class Pincode{
public static void main(String args[]) {
int[] arr = {110061, 110001,
110029, 110023,
110055, 110006,
110019, 110033};

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


{
int idx = i;
for (int j = i + 1; j < 8; j++)
{
if (arr[j] < arr[idx])
idx = j;
}

int t = arr[i];
arr[i] = arr[idx];
arr[idx] = t;
}

[Link]("Sorted Array:");
for (int i = 0; i < 8; i++)
{
[Link](arr[i] + " ");
}

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

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 KboatExamResult{
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]);
}
}}
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.

Inpu
Input Output
t

P[ ] Q[ ] R[ ]

4 19 4

6 23 6

1 7 1

2 8 2
Inpu
Input Output
t

3 3

10 10

19

23

import [Link];
public class Kboat3Arrays{
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] + " ");
}
}}
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 199 199 200


1987 1999 2006 2007 2009 2010
2 3 6 3

import [Link];
public class KboatGraduationYear{
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");
}}
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
Average Marks Remark

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);
}
}}
Define a class to accept values into an integer array of order 4 x 4 and check
whether it is a DIAGONAL array or not. An array is DIAGONAL if the sum of
the left diagonal elements equals the sum of the right diagonal elements. Print
the appropriate message.
Example:
3425
2523
5327
1371
Sum of the left diagonal element = 3 + 5 + 2 + 1 = 11
Sum of the right diagonal element = 5 + 2 + 3 + 1 = 11
import [Link];
public class KboatDiagonalDDA{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int[][] arr = new int[4][4];

[Link]("Enter elements for 4x4 DDA:");


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

int lSum = 0;
int rSum = 0;

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


lSum += arr[i][i];
rSum += arr[i][3 - i];
}

if (lSum == rSum) {
[Link]("The array is a DIAGONAL array.");
} else {
[Link]("The array is NOT a DIAGONAL array.");
}
}}
If arrays M and M + N are as shown below, write a program in Java to find the
array N.
M = {{-1, 0, 2}, M + N = {{-6, 9, 4},
{-3, -1, 6}, {4, 5, 0},
{4, 3, -1}} {1, -2, -3}}
public class KboatSubtractDDA{
public static void main(String args[]) {

int arrM[][] = {
{-1, 0, 2},
{-3, -1, 6},
{4, 3, -1}
};

int arrSum[][] = {
{-6, 9, 4},
{4, 5, 0},
{1, -2, -3}
};

int arrN[][] = new int[3][3];

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


for (int j = 0; j < 3; j++) {
arrN[i][j] = arrSum[i][j] - arrM[i][j];
}
}

[Link]("Array N:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
[Link](arrN[i][j]);
[Link](' ');
}
[Link]();
}
}}

You might also like