Java Exercises 9
1. Write a java program to generate the Fibonacci series starting with any numbers of your choice
together with a given number of items limit in the series.
2. Write a Java program to find the maximum and minimum value of an array.
3. Write a Java program to calculate the factorial of any positive non-zero integer value.
4. Write a Java program to sort values of an array in ascending order.
5. Write a Java program to check whether any positive non-integer value is:
a) Perfect number or not
b) Prime number or not
c) Palindrome or not
6. Any other program (of your choice) to practically test.
7. An onion array is an array that satisfies the following condition for all values of j and
k: if j>=0 and k>=0 and j+k=length of array and j!=k then a[j]+a[k] <= 10
Write a function named isOnionArray that returns 1 if its array argument is an onion array and
returns 0 if it is not.
Your solution must not use a nested loop (i.e., a loop executed from inside another loop).
Furthermore, once you determine that the array is not an onion array your function must return 0;
no wasted loops cycles please!
If you are programming in Java or C#, the function signature is int isOnionArray(int[ ] a)
If you are programming in C or C++, the function signature is int isOnionArray(int a[ ], int len) where
len is the number of elements in the array
1|Page
a. Examples
8. A number is called digit-increasing if it is equal to n + nn + nnn + … for some digit n between 1 and
9. For example 24 is digit-increasing because it equals 2 + 22 (here n =2)
Write a function called isDigitIncreasing that returns 1 if its argument is digitincreasingotherwise,
it returns 0.
The signature of the method is int isDigitIncreasing(int n)
Examples:
9. An array is zero-plentiful if it contains at least one 0 and every sequence of 0s is of length at least
4. Write a method named isZeroPlentiful which returns the number of zero sequences if its array
argument is zero-plentiful, otherwise it returns 0.
If you are programming in Java or C#, the function signature is int isZeroPlentiful(int[ ] a)
If you are programming in C or C++, the function signature is int isZeroPlentiful(int a[ ], int len)
where len is the number of elements in the array a.
2|Page
Examples:
10. Define the n-based integer rounding of an integer k to be the nearest multiple of n to k. If two
multiples of n are equidistant use the greater one. For example, the 4based rounding of 5 is 4
because 5 is closer to 4 than it is to 8, the 5-based rounding of 5 is 5 because 5 is closer to 5 that
it is to 10, the 4-based rounding of 6 is 8 because 6 is equidistant from 4 and 8, so the greater one
is used, the 13-based rounding of 9 is 13, because 9 is closer to 13 than it is to 0.
Write a function named doIntegerBasedRounding that takes an integer array and rounds all its
positive elements using n-based integer rounding.
A negative element of the array is not modified and if n <=0, no elements of the array are
modified. Finally you may assume that the array has at least two elements.
Hint: In integer arithmetic, (6/4) * 4 = 4
If you are programming in Java or C#, the function signature is void doIntegerBasedRounding(int[
] a, int n) where n is used to do the rounding
If you are programming in C or C++, the function signature is void doIntegerBasedRounding(int a[
], int n, int len) where n is used to do the rounding and len is the number of elements in the array
a.
3|Page
Examples:
4|Page