[Go to site: main page, start]

0% found this document useful (0 votes)
9 views48 pages

Java Array Manipulation Assignments

Uploaded by

Neelesh Baghel
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)
9 views48 pages

Java Array Manipulation Assignments

Uploaded by

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

Java Assignment 3

Q1. Given an array of integers (in a series) and we have to


find its
missing elements (there will be a missing element)

Input/Output:

Input array: 1, 2, 3, 4, 6, 7
Output:
Missing element is: 5
Solution:-
class A31{
public static void main(String args[]){
int[] arr={1,2,3,4,6,7};

int actualsum=0;

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

}
int expectedsum= 1+2+3+4+5+6+7;
int missingnum = expectedsum-actualsum;
[Link]("Missing Number : "+missingnum);

}
}
Output:-
Q2. Given an array of integers and we
have to find their average .
Input/Output:
Enter number of elements you want in
array : 10
Enter all the elements :
65
45
25
65
84
74
74
15
36
Sum of the array elements is : 579
Average of the array elements is : 57.9
Solution:-
import [Link].*;
class A32{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter number of element you want in array : ");
int num=[Link]();
[Link]("Enter all element :");

int sum=0;
float average;

int[] arr=new int[num];

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

}
average=(float)sum/num;

[Link]("Sum of the array elements is : "+sum);


[Link]("Average of array element : "+average);

}
}
Output:-
Q3. Move all zero at the end of the array
Given an integer array with zeros (0’s) and we have to move all zeros at
the end of the array
Input/Output:
Input array: 5, 1, 6, 0, 0, 3, 9, 0, 6, 7, 8, 12, 10, 0, 2
After moving 0 at the end
Output array: 5, 1, 6, 3, 9, 6, 7, 8, 12, 10, 2, 0, 0, 0, 0
Solution:-
class A33{
public static void main(String args[]){
int[] arr={5,1,6,0,0,3,9,0,6,7,8,12,10,0,2};

int index=0;
for(int i=0;i<[Link];i++){
if(arr[i] != 0){
arr[index++]=arr[i];
}
}
while(index<[Link]){
arr[index++]=0;
}
[Link]("After moving 0 at the end : ");
for(int num : arr){
[Link](num+" ");
}

}
}
Output:-

Q4. Delete a specific element from a one dimensional array


Given an array and an element to delete and we have to delete it from
Array.
Input/Output:
Input:
Given array (elements will be read in program): 10 20 30 40 50
Enter element to delete: 40
Output:
Array elements after deleting the element: 10 20 30 50
Solution:-
class A34{
public static void main(String args[]){
int[] arr={10,20,30,40,50,60};
int ed=40;
int index =-1;
for(int i=0;i<[Link];i++){
if(arr[i]==ed){
index=i;
break;
}

}
if(index==-1){
[Link]("Element not found in the array");
} else{
for(int i=index;i<[Link]-1;i++){
arr[i]=arr[i+1];
}

[Link]("Array element after deleting the element ");


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

}
}
Output:-
Q5. Print EVEN and ODD elements from an array
Given a one dimensional array and we have to print its EVEN and ODD
elements separately.
Input/Output:
Input:
Given array (elements will be read in program): 10 11 12 13 14
Output:
Odd numbers in the array are : 10 12 14
Even numbers in the array are : 11 13
Solution:-
import [Link].*;
class A35{
public static void main(String args[]){

Scanner obj=new Scanner([Link]);


int[] arr=new int[5];
int[] evenarr=new int[5];
int[] oddarr=new int[5];
int evencount=0;
int oddcount=0;
for(int i=0;i<[Link];i++){
[Link]("Enter array : ");
arr[i]=[Link]();
}

for(int i =0;i<[Link];i++){
if(arr[i]%2==0){
evenarr[evencount++]=arr[i];
} else{
oddarr[oddcount++]=arr[i];
}
}
for(int i=0;i<evencount;i++){
[Link](evenarr[i]+" ");
}
[Link]();
for(int i=0;i<oddcount;i++){
[Link](oddarr[i]+" ");
}
[Link]();

}
}
Output:-
Q6. Merge two one dimensional arrays
Given two one-dimensional arrays and we have to merge them using
java program.
Input/Output:
Input:
Array 1 (elements will be read in program): 1 2 3 4 5 6 7 8 9 10
Array 2 (elements will be read in program): 11 12 13 14 15
Output:
New array (After merging elements)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Solution:-
class A36{
public static void main(String args[]){
int[] arr1={1,2,3,4,5,6,7,8,9,10};
int[] arr2={11,12,13,14,15};

int[] merge= new int[[Link]+[Link]];


int m=0;
for(int i=0;i<[Link];i++){
merge[m]=arr1[i];
m++;
}
for(int i=0;i<[Link];i++){
merge[m]=arr2[i];
m++;
}
[Link]("New array after merging : ");
for(int i=0;i<[Link];i++){
[Link](merge[i]+" ");
}
}
}
Output:-

Q7. Read and print a two dimensional array


Read number of rows and columns, array elements for two dimensional
array and print in matrix format using java program.
Input/Output:
Enter number of rows: 3
Enter number of columns: 3
Enter elements
1
2
3
4
5
6
7
8
9
Output:
Matrix is:
123
456
789
Solution:-
import [Link].*;
class A37{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of row : ");
int r=[Link]();
[Link]("Enter number of column : ");
int c=[Link]();

int[][] arr=new int[r][c];


for(int i=0;i<[Link];i++){
for(int j=0;j<arr[0].length;j++){
[Link]("Enter element at position (" + i + ", " +
j + "): ");
arr[i][j]=[Link]();
}
}
for(int i=0;i<[Link];i++){
for(int j=0;j<arr[0].length;j++){
[Link](arr[i][j]+"\t");
}
[Link]();
}

}
}
Output:-
Q9. Remove duplicate elements from an array
Given an array of integers and we have to remove duplicate elements
using java program.
Input/Output:
Input array elements:
1, 2, 3, 1, 2, 3, 4
Output:
Elements after removing duplicates
1, 2, 3, 4
Solution:-
import [Link].*;
class A39{
public static void main(String args[]){
Scanner obj= new Scanner([Link]);
[Link]("Enter value of array : ");
int arr[]=new int[7];
int n=[Link]-1;
int m=0;
for(int i=0;i<[Link];i++){
arr[i]=[Link]();
}
for(int i=0;i<[Link];i++){
for(int j=i+1;j<n;j++){
if(arr[j]==arr[i]){
m=arr[n];
arr[n]=arr[j];
arr[j]=m;
n--;
}
}
}
for(int i=0;i<n;i++){
[Link](arr[i]+" ");
}

}
}
Output:-
Q10. Find second largest element in an array
Given an array of N integers and we have to find its second largest
element using Java program.
Input/Output:
Input:
Enter number of elements: 4
Output:
Second largest element in: 45
Solution:-
import [Link].*;
class A310{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of element : ");
int num=[Link]();
int arr[]=new int[num];
int max=Integer.MIN_VALUE;
int smax=Integer.MIN_VALUE;

for(int i=0;i<[Link];i++){
[Link]("Input element : ");
arr[i]=[Link]();
}
for(int i=0;i<[Link];i++){
if(arr[i]>max){
smax=max;
max=arr[i];
} else if(arr[i]>smax && arr[i]!=max){
smax=arr[i];
}

}
[Link]("Second Largest element is : "+smax);
}
}
Output:-

Q11. Find second smallest element in an array


Given an array of N integers and we have to find its second
minimum/smallest element using Java program.
Input/Output:
Input:
Enter number of elements: 4
Input elements: 45, 25, 69, 40
Output:
Second smallest element in: 40
Solution:-
import [Link].*;
class A311{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of element : ");
int num=[Link]();
int arr[]=new int[num];
int min=Integer.MAX_VALUE;
int smin=Integer.MAX_VALUE;

for(int i=0;i<[Link];i++){
[Link]("Input element : ");
arr[i]=[Link]();
}
for(int i=0;i<[Link];i++){
if(arr[i]<min){
smin=min;
min=arr[i];
} else if(arr[i]<smin && arr[i]!=min){
smin=arr[i];
}

}
[Link]("Second Smallest element is : "+smin);
}
}
Output:-
Q12. Find smallest element in an array
Given an array of N integers and we have to find its minimum/smallest
element using Java program.
Input/Output:
Input:
Enter number of elements: 4
Input elements: 45, 25, 69, 40
Output:
Smallest element in: 25
Solution:-
import [Link].*;
class A312{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of element : ");
int num=[Link]();
int arr[]=new int[num];
int min=Integer.MAX_VALUE;
for(int i=0;i<[Link];i++){
[Link]("Input element : ");
arr[i]=[Link]();
}
for(int i=0;i<[Link];i++){
if(arr[i]<min){
min=arr[i];
}

}
[Link]("Smallest element is : "+min);
}
}
Output:-

Q13. Count total positives, negatives and zeros from an array


Given an array of integers and we have to count total negatives,
positives and zeros using java program.
Input/Output:
Input:
Array elements: 20, -10, 15, 00, -85
Output:
Positive Numbers are: 2
Negative Numbers are: 2
Zeros are: 1
Solution:-
class A313{
public static void main(String args[]){
int[] arr={20,-10,15,00,-85};

int pcount=0;
int ncount=0;
int zcount=0;
for(int i=0;i<[Link];i++){
if(arr[i]>0){
pcount++;
} else if(arr[i]<0){
ncount++;
} else if(arr[i]==0){
zcount++;
}
}
[Link]("Posotive number are : "+pcount);
[Link]("Negative number are : "+ncount);
[Link]("Zero are : "+zcount);

}
}
Output:-
Q14. Access elements of character array using for each loop
In this program, we will create an array of characters and access its
elements using for each loop and print it on the screen.
Input/Output:
Input:
char charArray[] = {&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;,
&#39;E&#39;, &#39;F&#39;};
Output:
Array elements:
A
B
C
D
E
F
Solution:-
class A314{
public static void main(String args[]){

char[] arr={'A','B','C','D','E'};
int i;

for(char x:arr){
[Link](x);
}

}
}
Output:-

Q16. Search an item into the array using linear search


The linear searching algorithm is used to find the item in an array, This
algorithm consists the checking every item in the array until the
required item is found or till the last item is in the array. The linear
search algorithm is also known as a sequential algorithm.

In this program, we will create an array of integers then we will search


an item into the array using linear search and print position of the item
in the array.
Input/Output:
Input:
Enter array elements: 10 20 30 40 50
Enter item to search: 40
Output:
Item found at index 3.
Solution:-
import [Link].*;
class A316{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter array elements : ");
int num[]=new int[5];

for(int i=0;i<[Link];i++){
num[i]=[Link]();
}
[Link]("Enter item to serach : ");
int item=[Link]();
Boolean isFound=false;
for(int i=0;i<[Link];i++){
if(num[i]==item){
[Link]("Element found at position "+i);
isFound=true;
break;
}
}

}
}
Output:-

Q17. Search an item in an array using binary search


The binary searching algorithm is used to find the item in a sorted array
with the minimum number of comparisons, in this algorithm key
element compares with the middle index item.
In this program, we will create an array of sorted integers then we will
search an item into an array using binary search and print the position
of
the item in the array.
Input/Output:
Input:
int arr[] = {10, 20, 30, 40, 50};
Output:
Enter item to search: 30
Item found at index 2.
Solution:-
import [Link].*;
class A317{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
int[] arr={10,20,30,40,50};
Boolean isFound=false;
[Link]("Enter item to search : ");
int item=[Link]();

for(int i=0;i<[Link];i++){
if(arr[i]==item){
[Link]("Item found at index "+i);
isFound=true;
break;
}
}

}
}
Output:-

Q18. This is the Java Program to Find Repeated Elements and the
Frequency of Repetition.
Problem Description
Given an array of integers, find and print the repeated elements and
their
frequency of repetition.
Example:
Array: [1,2,3,4,5,5,3]
Solution:-
class A318{
public static void main(String args[]){
int n=1234553;
int freq[]=new int[6];

while(n!=0){
int r=n%10;
freq[r]++;
n=n/10;
}
[Link]("Frequecy with number ");
for(int i=0;i<[Link];i++){
if(freq[i]!=0){
[Link]("frequency of "+i+" is = "+freq[i]);
}
}
}
}

Output:-

Q22. Given an array of integers, print the even numbers present at even
index numbers.
Example:
Array = [2,1,4,4,5,7]
Output = 2 4
Solution:-
class A322{
public static void main(String args[]){
int[] arr={2,1,4,4,5,7};
[Link]("Output is : ");
for(int i=0;i<[Link];i++){
if(i%2==0 && arr[i]%2==0){

[Link](arr[i]+" ");
}
}
}
}
Output:-

Q25. This is a Java Program to Sort Names in an Alphabetical Order.


Enter size of array and then enter all the names in that array. Now with
the help of compareTo operator we can easily sort names in
Alphabetical Order.
Enter number of names you want to enter:5
Enter all the names:
bryan
adam
rock
chris
scott
Names in Sorted Order:adam,bryan,chris,rock,scott
Solution:-
import [Link].*;
class A325{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of names you want to enter : ");
int n=[Link]();
String[] names=new String[n];
[Link]("Enter all names : ");
for(int i=0;i<n;i++){
names[i]=[Link]();
}

for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(names[i].compareTo(names[j])>0){
String temporay=names[i];
names[i]=names[j];
names[j]=temporay;
}
}
}
[Link]("Names in sorted order : ");
for(int i=0;i<n;i++){
[Link](names[i]);
}

}
}
Output:-

Q27. Problem Description


Given an array of integers, shift all the zeroes present in it to the end.
Example:
Array = [1 0 2 3 0 4]
Output
Array = [1 2 3 4 0 0]
Solution:-
class A327{
public static void main(String args[]){
int [] arr={1,0,2,3,0,4};
int index=0;

for(int i=0;i<[Link];i++){
if(arr[i]!=0){
arr[index++]=arr[i];
}
}
while(index<[Link]){
arr[index++]=0;
}
[Link]("Array element after shif to end : ");
for(int num : arr){
[Link](num+" ");
}

}
}
Output:-

Q29. Given an array arr of n elements that is first strictly increasing and
then maybe strictly decreasing, find the maximum element in the array.
Note: If the array is increasing then just print the last element will be
the
maximum value.
Example:
Input: array[]= {5, 10, 20, 15}
Output: 20
Explanation: The element 20 has neighbors 10 and 15, both of them are
less than 20.
Input: array[] = {10, 20, 15, 2, 23, 90, 67}
Output: 20 or 90
Solution:-

class A329 {
public static void main(String args[]){
int[] arr={5,10,20,15};
int[] arr1={10,20,15,2,23,90,67};
for(int i=1;i<[Link];i++){
if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){
[Link]("Output : "+arr[i]);

}
}
[Link]("Output : ");
for(int i=1;i<[Link];i++){
if(arr1[i]>arr1[i-1] && arr1[i]>arr1[i+1]){

[Link](arr1[i]+" ");
}
}
}
}
Output:-

Q30. An array contains both positive and negative numbers in random


order. Rearrange the array elements so that all negative numbers
appear
before all positive numbers.
Examples :
Input: -12, 11, -13, -5, 6, -7, 5, -3, -6
Output: -12 -13 -5 -7 -3 -6 11 6 5
Solution:-
class A330{
public static void main(String args[]){
int arr[]={-12,11,-13,-5,6,-7,5,-3,-6};
int a=0;
for(int i=0;i<[Link];i++){
if(arr[i]<0){
int tem=arr[i];
arr[i]=arr[a];
arr[a]=tem;
a++;
}
}
[Link]("Output : ");
for(int i=0;i<[Link];i++){
[Link](arr[i]+" ");
}

}
}
Output:-

Q31. Given an array arr[] of non-negative integers and an integer sum,


find a subarray that adds to a given sum.
Note: There may be more than one subarray with sum as the given sum,
print first such subarray.
Examples:
Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33
Output: Sum found between indexes 2 and 4
Solution:-
class A331{
public static void main(String args[]){
int arr[]={1,4,20,3,10,5};
int sum=0;

for(int i=2;i<=[Link]-2;i++){
sum +=arr[i];
}
[Link]("Sum found between indexes 2 and 4 : "+sum);

}
}
Output:-

Q32. Find the first repeating element in an array of integers


Given an array of integers arr[], The task is to find the index of first
repeating element in it i.e. the element that occurs more than once and
whose index of the first occurrence is the smallest.
Examples:
Input: arr[] = {10, 5, 3, 4, 3, 5, 6}
Output: 5
Solution:-
import [Link].*;
class A332{
public static void main(String args[]){
int[] arr={10,5,3,4,3,5,6};
Set<Integer> seen = new HashSet<>();
for(int i=0;i<[Link];i++){
if([Link](arr[i])){
[Link]("First repeating element : "+arr[i]);
break;
}
else{
[Link](arr[i]);
}
}

}
}
Output:-

Q35. Program to find the smallest and largest number in an integer


array
Output: Given integer array : [-20, 34, 21, -87, 92, 2147483647]
Largest number in array is : 2147483647
Smallest number in array is : -87
Solution:-
class A335{
public static void main(String args[]){
int[] arr={-20,34,21,-87,92,100};
int largest=arr[0];
int smallest=arr[0];

for(int i=0;i<[Link];i++){
if(arr[i]>largest){
largest=arr[i];

}
else if(arr[i]<smallest){
smallest=arr[i];

}
}
[Link]("Largest number in array : "+largest);
[Link]("Smallest number in array : "+smallest);
}
}
Output:-
Q36. Reverse int and String array
Output: Original int array : [101, 102, 103, 104, 105]
reversed int array : [105, 104, 103, 102, 101]
Solution:-
class A336{
public static void main(String args[]){
int [] arr={101,102,103,104,105};

[Link]("Reversed int array : ");


for(int i=4;i>=0;i--){
[Link](arr[i]);
if(i!=0){
[Link](",");
}
}

}
}
Output:-

Q37. Write a program to test if an array contains a specific value.


Solution:-
class A337{
public static void main(String args[]){
int arr[]={1789,2035,1899,1456,2013};

int x=2013;
for(int i=0;i<[Link];i++){
if(arr[i]==x){
[Link]("True");
}
}

}
}
Output:-

Q38. Write a program to find the index of an array element.


Solution:-
class A338{
public static void main(String args[]){
int arr[]={25,14,56,15,36};

int x=25;
[Link]("Index position of 25 is : ");
for(int i=0;i<[Link];i++){
if(arr[i]==x){
[Link](i);
}
}

}
}
Output:-
Q39. Write a program to remove a specific element from an array.
Solution:-
class A339{
public static void main(String args[]){
int arr[]={25,14,56,15,36};
int ed=25;
int index =-1;
for(int i=0;i<[Link];i++){
if(arr[i]==ed){
index=i;
break;
}

}
if(index==-1){
[Link]("Element not found in the array");
} else{
for(int i=index;i<[Link]-1;i++){
arr[i]=arr[i+1];
}

[Link]("Array element after deleting the element : ");


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

}
}
Output:-
Q40. Matrix Multiplication Program
In case of matrix multiplication, one row element of first matrix is
multiplied by all columns of second matrix.
Solution:-
import [Link].*;
class A340{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
int[][] arr=new int[3][3];
int[][] arr1=new int[3][3];
int[][] arr2=new int[3][3];
[Link]("Enter element of Matrix 1");
for(int r=0;r<[Link];r++){
for(int c=0;c<[Link];c++){
[Link]("Enter element if Index "+r+","+c+": ");
arr[r][c]=[Link]();
}
}
[Link]("Enter element of Matrix 2");
for(int r=0;r<[Link];r++){
for(int c=0;c<[Link];c++){
[Link]("Enter element if Index "+r+","+c+": ");
arr1[r][c]=[Link]();
}
}

for(int r=0;r<[Link];r++){
for(int c=0;c<[Link];c++){
arr2[r][c]=arr[r][c]*arr1[r][c];
[Link](arr2[r][c]+" ");
}
[Link]();
}
}
}
Output:-

Q41. Passing Division Program


Enter marks out of 100
Enter marks of math=45
Enter marks of english=65
Enter marks of science=78
Enter marks of art=75
Enter marks of computer=42
Total marks out of 500=305
Percent of marks=61
First Division
Solution:-
class A341{
public static void main(String args[]){
int[] arr={45,65,78,75,42,};
int total=0;
int percent;
String[] subject={"Math","English","Science","Art","commerce"};
[Link]("Marks in each subject ");
for(int i=0;i<[Link];i++){
[Link](subject[i]+" : "+arr[i]);
total += arr[i];
}
[Link]("Total marks out of 500 : "+total);
[Link]("Percent of marks : "+(total/[Link]));
[Link]("First Division");

}
}
Output:-
Q42. Find second largest element in an array
Given an array of N integers and we have to find its second largest
element
Input/Output:
Input:
Enter number of elements: 4
Input elements: 45, 25, 69, 40
Output:
Second largest element in: 45
Solution:-
import [Link].*;
class A342{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of elements : ");
int n=[Link]();
int[] arr=new int[n];

[Link]("Input element : ");


for(int i=0;i<n;i++){
arr[i]=[Link]();
}
int largest=Integer.MIN_VALUE;
int secondlargest=Integer.MIN_VALUE;
for(int i=0;i<n;i++){
if(arr[i]>largest){
secondlargest=largest;
largest=arr[i];
} else if(arr[i]>secondlargest && arr[i]!=largest){
secondlargest=arr[i];
}
}
[Link]("Second largest element is : "+secondlargest);
}
}
Output:-

Q43. Find second smallest element in an array


Given an array of N integers and we have to find its second
minimum/smallest element
Input/Output:
Input:
Enter number of elements: 4
Input elements: 45, 25, 69, 40
Solution:-
import [Link].*;
class A343{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of elements : ");
int n=[Link]();
int[] arr=new int[n];
[Link]("Input elements : ");
for(int i=0;i<n;i++){
arr[i]=[Link]();
}
int smallest=Integer.MAX_VALUE;
int secondsmallest=Integer.MAX_VALUE;
for(int i=0;i<n;i++){
if(arr[i]<smallest){
secondsmallest=smallest;
smallest=arr[i];
}
}
[Link]("Second smallest element is : "+smallest);
}
}
Output:-

Q44. Count total positives, negatives and zeros from an array


Given an array of integers and we have to count total negatives,
positives and zeros
Input/Output:
Input:
Array elements: 20, -10, 15, 00, -85
Output:
Positive Numbers are: 2
Negative Numbers are: 2
Zeros are: 1
Solution:-
class A344{
public static void main(String args[]){
int arr[]={20,-10,15,00,-85};
int pcount=0;
int ncount=0;
int zcount=0;

for(int i=0;i<[Link];i++){
if(arr[i]>0){
pcount++;
}
else if(arr[i]<0){
ncount++;
}
else if(arr[i]==0){
zcount++;
}
}
[Link]("Positive Number are : "+pcount);
[Link]("Negative Number are : "+ncount);
[Link]("Zero Number are : "+zcount);

}
}
Output:-
Q45. Find the length of an array
In this program, we will create an array of 10 integers then we will find
the length of the array
Input/Output:
Input:
int[] intArr = new int[10];
Output:
Length of array is: 10
Solution:-
class A345{
public static void main(String args[]){
int[] arr=new int[10];
[Link]("length of array : "+[Link]);
}
}

Output:-

Q48. Program to Find the Largest Two Numbers in a Given Array.


Enter size of array and then enter all the elements of that array.
Enter no. of elements you want in array:5
Enter all the elements:
1
5
4
8
7
The First largest is 8
The Second largest 7
Solution:-
import [Link].*;
class A348{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of element you want to array : ");
int n=[Link]();

int[] arr=new int[n];

[Link]("Enter all elements : ");


for(int i=0;i<n;i++){
arr[i]=[Link]();
}
int flargest=Integer.MIN_VALUE;
int slargest=Integer.MIN_VALUE;

for(int i=0;i<n;i++){
if(arr[i]>flargest){
slargest=flargest;
flargest=arr[i];
} else if(arr[i]>slargest && arr[i]!=flargest){
slargest=arr[i];
}
}
[Link]("The first largest is : "+flargest);
[Link]("The second largest is : "+slargest);

}
}
Output:-

Q49. Program to Separate Odd and Even Numbers from an Array


Program to Put Even &amp; Odd Elements of an Array in 2 Separate
Arrays.
Enter no. of elements you want in array:8
Enter all the elements:
1
2
3
4
5
6
7
8
Odd:1,3,5,7
Even:2,4,6,8
Solution:-
import [Link].*;
class A349{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of elements you want in array : ");
int n=[Link]();
int[] arr=new int[n];

[Link]("Enter all elements : ");


for(int i=0;i<n;i++){
arr[i]=[Link]();
}
[Link]("Even values of array : ");
for(int i=0;i<n;i++){
if(arr[i]%2==0){
[Link](arr[i]+" ");
}

}
[Link]();
[Link]("Odd values of array : ");
for(int i=0;i<n;i++){
if(arr[i]%2!=0){
[Link](arr[i]+" ");
}

}
[Link]();
}
}

Output:-

You might also like