Take 10 integer inputs from user and store them in an array and print them on screen.
importjava.util.*;classAns{publicstaticvoidmain(String[]args){Scanners=newScanner(System.in);int[]z=newint[10];for(inti=0;i<z.length;i++){System.out.println("Print the value of z["+i+"]");z[i]=s.nextInt();}for(inti=0;i<z.length;i++){System.out.println("The value of z["+i+"] is "+z[i]);}}}
2.
Take 10 integer inputs from user and store them in an array. Again ask user to give a number. Now, tell user whether that number is present in array or not.
3.
Take 20 integer inputs from user and print the following: number of positive numbers number of negative numbers number of odd numbers number of even numbers number of 0s.
importjava.util.*;classAns{publicstaticvoidmain(String[]args){Scanners=newScanner(System.in);int[]z=newint[20];intpos=0;intneg=0;intodd=0;inteven=0;intzero=0;for(inti=0;i<z.length;i++){System.out.println("Print the value of z["+i+"]");z[i]=s.nextInt();if(z[i]>0){pos++;}elseif(z[i]<0){neg++;}else{zero++;}if(z[i]%2==0){even++;}else{odd++;}}System.out.println("Positive : "+pos+"\nNegative : "+neg+"\nZero : "+zero+"\nodd : "+odd+"\neven : "+even);}}
4.
Take 10 integer inputs from user and store them in an array. Now, copy all the elements in an another array but in reverse order.
importjava.util.*;classAns{publicstaticvoidmain(String[]args){Scanners=newScanner(System.in);int[]a=newint[10];int[]b=newint[10];for(inti=0;i<a.length;i++){System.out.println("Enter the value of a["+i+"]");a[i]=s.nextInt();}intj=0;for(inti=b.length-1;i>=0;i--){b[i]=a[j];j++;}for(inti=0;i<b.length;i++){System.out.println("The value of b["+i+"] is "+b[i]);}}}
5.
Write a program to find the sum and product of all elements of an array.
6.
Initialize and print all elements of a 2D array.
7.
Find largest and smallest elements of an array.
importjava.util.*;classAns{publicstaticvoidmain(String[]args){Scanners=newScanner(System.in);int[]a=newint[10];for(inti=0;i<a.length;i++){System.out.println("Enter the value of a["+i+"]");a[i]=s.nextInt();}intlargest=a[0];intsmallest=a[0];for(inti=0;i<a.length;i++){if(a[i]>largest)largest=a[i];if(a[i]<smallest)smallest=a[i];}System.out.println("Largest is "+largest+" and smallest is "+smallest);}}
8.
Write a program to check if elements of an array are same or not it read from front or back. E.g.-
Take an array of 10 elements. Split it into middle and store the elements in two dfferent arrays. E.g.- INITIAL array :
58
24
13
15
63
9
8
81
1
78
After spliting :
58
24
13
15
63
9
8
81
1
78
10.
Consider an integer array, the number of elements in which is determined by the user.
The elements are also taken as input from the user. Write a program to find those pair
of elements that has the maximum and minimum difference among all element pairs.
maximum difference = higest-lowest minimum difference = second lowest - lowest
11.
If the input array is [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60], your program should be able to find that the subarray lies between the indexes 3 and 8.
Level 2
1.
Take an array of length n where all the numbers are nonnegative and unique. Find the element in the array possessing the highest value. Split the element into two parts where first part contains the next highest value in the array and second
part hold the required additive entity to get the highest value. Print the array where the highest value get splitted into those two parts.
Sample input: 4 8 6 3 2
Sample output: 4 6 2 6 3 2
Sorting refers to arranging data in a particular format. Sort an array of integers in ascending order. One of the algorithm is selection sort. Use below explanation of selection sort to do this. INITIAL ARRAY :
2
3
1
45
15
First iteration : Compare every element after first element with first element and if it is larger then swap. In first iteration, 2 is larger than 1. So, swap it.
1
3
2
45
15
Second iteration : Compare every element after second element with second element and if it is larger then swap. In second iteration, 3 is larger than 2. So, swap it.
1
2
3
45
15
Third iteration : Nothing will swap as 3 is smaller than every element after it.
1
2
3
45
15
Fourth iteration : Compare every element after fourth element with fourth element and if it is larger then swap. In fourth iteration, 45 is larger than 15. So, swap it.