0% found this document useful (0 votes)
7 views12 pages

Java Array Operations and Solutions

The document contains multiple Java programming exercises focused on array manipulation, including linear search, bubble sort, and filtering even and odd integers. It also includes tasks for identifying perfect and buzz numbers, correcting code errors, and analyzing code outputs. Each section provides code snippets and expected outputs for various array-related operations.

Uploaded by

sharmaakshdha01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Java Array Operations and Solutions

The document contains multiple Java programming exercises focused on array manipulation, including linear search, bubble sort, and filtering even and odd integers. It also includes tasks for identifying perfect and buzz numbers, correcting code errors, and analyzing code outputs. Each section provides code snippets and expected outputs for various array-related operations.

Uploaded by

sharmaakshdha01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ARRAYS WORKSHEET SOLUTION

1. Write a program to perform the Linear search on 10 names of country and its capital
stored in two SDAs . Type the name of the country whose capital is to be searched. If the
country found then the name of the country and its capital needs to be displayed with its
position __” otherwise display “country not in the list.”

import [Link].*;
public class prog_2
{
static void main()
{
Scanner ob= new Scanner([Link]);
String co[]=new String[10];
String cp[]=new String[10];
String c;
int i,k=0;
boolean x=false;
for(i=0;i<10;i++)
{
[Link]("Enter the country");
co[i]=[Link]();
[Link]("Enter the capital");
cp[i]=[Link]();
[Link]();
}
[Link]("Enter the country to be searched");
c=[Link]();
for(i=0;i<10;i++)
{
if(co[i].equalsIgnoreCase(c))
{
k++;
x=true;
break;
}
}
if(x)
{
[Link]("The capital of "+c+" is "+cp[i]);
[Link]("Position= "+k);
}
else
[Link]("Country not in the list");
}
}
}
}

2. Write a program to input an integer array A[ ] of n size. Sort the array in ascending order
using bubble sort . Then input another number from the user and replace all the numbers
less than that inputted number by their reverse.
Example :
If A[ ] = {38, 25, 16, 91, 5, 12} then, array after sorting is {5, 12, 16, 25, 38, 91}
If the number entered = 20 then, after replacing all the numbers less than 20 with their
reverse, final Output is {5, 21, 61, 25, 38, 91}

import [Link].*;
public class prog_4
{
static void main()
{
Scanner ob= new Scanner([Link]);
int n;
int i,j,t;
int k,r,s=0;
[Link]("Enter the length of array A");
n=[Link]();
int A[]=new int[n];
[Link]("Enter the values for array A");
for(i=0;i<n;i++)
{
A[i]=[Link]();
}

for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(A[j]>A[j+1])
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
[Link]("Array after sorting is");

for(i=0;i<n;i++)
{
[Link](A[i]+",");
}
[Link]();
[Link]("Enter the number for change");
k=[Link]();

for(i=0;i<n;i++)
{
if(A[i]<k)
{
s=0;
while(A[i]>0)
{
r=A[i]%10;
s=s*10+r;
A[i]=A[i]/10;
}
A[i]=s;
}
}
[Link]("New array is");
for(i=0;i<n;i++)
{
[Link](A[i]+",");
}
}
}
5. Write a program to input an integer array A[ ] of n size. Store all even integers of array
A[ ] from left to right and all odd integers from right to left in another array B[ ]. Print both
the arrays.
Example :
If A[ ] = {3, 6, 9, 5, 12, 14, 8, 18, 7, 21, 10, 4} then,
B[ ] = {6, 12, 14, 8, 18, 10,4, 21, 7, 5, 9, 3}

import [Link].*;
public class prog_5
{
static void main()
{
Scanner ob= new Scanner([Link]);
int n,i;
int k,k1;
[Link]("Enter the size of array A");
n=[Link]();
k=0;k1=n-1;
int A[]=new int[n];
int B[]=new int[n];
[Link]("Enter the values of array A");
for(i=0;i<n;i++)
{
A[i]=[Link]();
}
[Link]("array A is ");
for(i=0;i<n;i++)
{
[Link](A[i]+",");
}
[Link]();
for(i=0;i<n;i++)
{
if(A[i]%2==0)
{
B[k]=A[i];
k++;
}
else
{
B[k1]=A[i];
k1--;
}
}
[Link]("Array B is");
for(i=0;i<n;i++)
{
[Link](B[i]+",");
}
}
}
6. Write a program to accept a set of 20 integer in SDA . Using a menu driven display the
following as per user choice
a) find and display all perfect number in the array. (sum of the factors excluding
the number is equal to the number input)
b) find and display all the buzz number in the array (A number divisible by 7 or
ends with 7)

import [Link].*;
public class prog_6
{
static void main()
{
Scanner ob= new Scanner([Link]);
int arr[]=new int[20];
int i,j,c;
int s;
[Link]("Enter 20 values for the array");
for(i=0;i<20;i++)
{
arr[i]=[Link]();
}
[Link]("Enter 1 for perfect numbers");
[Link]("Enter 2 for buzz numbers");
c=[Link]();
switch(c)
{
case 1:
[Link]("The perfect numbers in the array are:");
for(i=0;i<20;i++)
{
s=0;
for(j=1;j<arr[i];j++)
{
if(arr[i]%j==0)
s=s+j;
}
if(s==arr[i])
[Link](arr[i]+" ");
}
break;
case 2:
[Link]("The buzz numbers in the array are");
for(i=0;i<20;i++)
{
if(arr[i]%7==0 || arr[i]%10==7)
[Link](arr[i]+",");
}
break;
default:
[Link]("Invalid input");
}
}
}
3. Rectify the errors from the following statement and rewrite the corrected
statements.
int a [5] = {10, 7, 9, 23, 67,100};

int a[ ]={10,7,9,23,67,100};

4. Give output of the following code.


int x[]={6, -2, 2, 3, 5, 6};
int s=0;
for (int i = 0; i<6; i+=2)
s = s + (x[i] + x[i+1]);
[Link](s);

WORKING:
i x[i] x[i+1] s
0 6 -2 4
2 2 3 5+4=9
4 5 6 9+11=20
OUTPUT:
20

5. int s, i;
int a [ ] = {2, 4, 6, 8};
for (i =0; i<=3; i++)
{
s= a[i] + a [3-i];
[Link] (“Sum =”+s);
}

WORKING:
i s
0 2+8=10
1 4+6=10
2 6+4=10
3 8+2=10

OUTPUT:
Sum=10
Sum=10
Sum=10
Sum=10

6. char arr[ ]= { 'C','O','M','P','U','T','E','R'};


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

WORKING:
i j j<=I o/p
0 0 true C
1 false
1 0 true C
1 true O
2 false
2 true C
1 true O
2 false M
(and so on till i=7)

OUTPUT:
C
CO
COM
COMP
COMPU
COMPUT
COMPUTE
COMPUTER
7. int arr[ ]={0,1,2,3,4,5,6,7,8,9};
int n=6;
n=arr[arr[n]/2];
[Link](arr[n]/2);

WORKING:
n n=arr[arr[n]/2] o/p
6 3 1

OUTPUT:
1

8. int a[]={1,2,3,4,5};
[Link]([Link]
th + 10);
[Link](a[0]+a
[4]/5);

OUTPUT:
152

9. int a[]={ 10,20,30,40,50};


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

WORKING:
i a[i+1]=a[i] o/p
0 a[1]=10 10
1 a[2]=10 10
2 a[3]=10 10
3 a[4]=10 10
OUTPUT:
10101010

10. char arr[ ]= { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’};


[Link](arr[0]+ [Link]);
[Link](arr[1]+arr[2]);
[Link](arr[0]==(arr[0]-32));

WORKING:
97+5=102
b+c=98+99=197
a==A false

OUTPUT:
102197false

11. char ch[]={ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’};


[Link](ch);

OUTPUT:
abcde

12. int a[ ]={ 9,4,3,7,0};


for(int i=0 ; i<=3 ;i++)
a[i+1]=(a[i]%2= = 0 ? ++a[ i
] : --a[ i ]);
[Link](a[i]);

WORKING:
i a[i+1]=(a[i]%2==0?++a[i]:--a[i])
0 a[1]=8
1 a[2]=9
2 a[3]=8
3 a[4]=9

OUTPUT:
9

You might also like