Arrays Practice Assignment
Part - 1
Note: Create Array with User Input Using Scanner
Que 1. Write a program to declare, initialize, and display the
elements of an integer array.
Que 2. Write a program to take an array of integers and
traverse it from start to end. Display all the elements of the
array.
Input: arr = {1, 2, 3, 4, 5}
Output: 1 2 3 4 5
Que 3. Write a program to take an array of integers and
traverse it from end to start. (also known as Reverse
Traversing)
Input: arr = {1, 2, 3, 4, 5}
Output: 5 4 3 2 1
Que 4. Write a program to take an array and traverse it from
start to half.
Input: arr = {1, 2, 3, 4, 5, 6}
Output: 1 2 3
Que 5. Write a program to take an array and traverse it from
end to half.
Input: arr = {1, 2, 3, 4, 5, 6}
Output: 6 5 4
Que 6. Write a program to take an array and traverse it from
half to start.
Input: arr = {1, 2, 3, 4, 5, 6}
Output: 3 2 1
Que 7. Write a program to take an array and traverse it from
half to end.
Input: arr = {1, 2, 3, 4, 5, 6}
Output: 4 5 6
Que 8. Write a program to print minimum element of an
array.
Input: arr = {5, 7, 3, 9, 4}
Output: 3
Que 9. Write a program to print maximum element of an
array.
Input: arr = {5, 7, 3, 9, 4}
Output: 9
Que 10. Write a program to print span of an array.
Input: arr = {5, 7, 3, 9, 4}
Output: maximum element: 9, minimum element: 3
span = 9-3 = 6
Que 11. Write a program to print 2 arrays together using
single for loop.
Input: arr1 = {1, 2, 3, 4} arr2 = {5, 6, 7, 8}
Output: 1 5
26
37
48
Que 12. Write a program to print the sum of all elements in a
given array.
Input: arr = {1, 2, 3, 4, 5}
Output: 15
Que 13. Write a program to create a new array containing only the
even elements of a given array. The program should:
1. Take an input array of integers.
2. Create a new array to store only the even elements from
the input array.
3. Return the new array and display its contents.
Input: arr = {14, 9, 6, 20, 35}
Output: evenArr = {14, 6, 20}
Que 14. Write a program to create a new array containing
only the odd elements of a given array. The program should:
1. Take an input array of integers.
2. Create a new array to store only the odd elements from
the input array.
3. Return the new array and display its contents.
Input: arr = {14, 9, 6, 20, 35}
Output: oddArr = {9, 35}
Que 15. Write a program to print Sum of only even elements
of an array.
Input : arr = {1, 2, 3, 4, 5}
Output: sum = 6
Que 16. Write a program to print Sum of only odd elements
of an array.
Input : arr = {1, 2, 3, 4, 5}
Output: sum = 9
Que 17. Write a program to print product of only even
elements of an array.
Input : arr = {1, 2, 3, 4, 5}
Output: product = 8
Que 18. Write a program to print product of only odd
elements of an array.
Input : arr = {1, 2, 3, 4, 5}
Output: product = 15
Que 19. Write a program to count the number of even and
odd elements in an array.
Input: arr = {1, 2, 3, 4, 5}
Output: even=2, odd=3
Que 20. Write a program to find the Square and Cube of all
elements of an array and store the results in two separate
arrays. Return the new arrays.
Input: arr = {1, 2, 3, 4, 5}
Output: Square = {1, 4, 9, 16, 25}
Cube = {1, 8, 27, 64, 125}
Que 21. Write a program to find the Square Root and Cube
Root of all elements of an array and store the results in two
separate arrays. Return the new arrays.
Input: arr1 = {1, 4, 9, 16, 25}
Output: sqrt = {1, 2, 3, 4, 5}
Input: arr2 = {1, 8, 27, 64, 125}
Output: cbrt = {1, 2, 3, 4, 5}
Que 22. Write a program to store only two digit elements of
an array in new array. Return the new Array.
Input: arr = {1, 22, 309, 4, 55}
Output: arr = {22, 55}
Que 23. Write a program to print the number of digits
present in every integer element of an array.
Input: arr = {22, 309, 4, 55}
Output: 22 is a 2 digit number
309 is a 3 digit number
4 is a 1 digit number
55 is a 2 digit number
Que 24. Write a program to find and store the prime
elements of an array in a new array. The program should
return the new array containing only the prime numbers.
Input: arr = {11,22,33,41,59}
Output: prime = {11, 41, 59,}
Que 25. Write a program to find whether the given element
is present in an array or not.
Input: arr = {11, 21, 31, 41, 51}
num = 31
Output: Yes
Que 26. Write a program to print the number of occurrence
of a given element in an array.
Input: arr = {1, 2, 3, 3, 6, 3, 5, 3}
num = 3
Output: 4
Que 27. Write a program to print the number of occurrence
of every element in an array.
Input: arr = {1, 5, 2, 5, 2, 2, 1, 6}
Output: Occurrence of 1 is 2
Occurrence of 5 is 2
Occurrence of 2 is 3
Occurrence of 6 is 1
Que 28. Write a program to find and store the sum of
elements of 2 arrays Index wise in a new array. Return the
new array.(both the arrays should have equal number of
elements)
Input: arr1 = {10, 20, 30, 40}
arr2 = {1, 2, 3, 4}
Output: sum = {11, 22, 33, 44}
Que 29. Write a program to find and store the product of
elements of 2 arrays Index wise in a new array. Return the
new array.(both the arrays should have equal number of
elements)
Input: arr1 = {10, 20, 30, 40}
arr2 = {1, 2, 3, 4}
Output: 10 40 90 160
Que 30. Write a program to calculate the sum and product of
the elements of two arrays, both the arrays should have
unequal numbers of elements. For indexes where elements
are not present in one of the arrays, simply print the
remaining elements of the longer array as they are. The
results should be stored in new arrays and returned.
Input: arr1 = {10, 20, 30, 40, 50}
arr2 = {1, 2, 3}
Output: Sum: {11, 22, 33, 40, 50}
Product: {10, 40, 90, 40, 50}