Java Programs
Java Programs
4. Write a Java program to test if an array contains a specific value and find the index of the
element if it exists.
9. Write a Java program to find the duplicate values of an array of integer values.
10. Write a Java program to find the common elements between two arrays of integers.
11. Write a Java program to solve quadratic equations (use if, else if and else).
Test Data
Input a: 1
Input b: 5
Input c: 1
Expected Output:
The roots are -0.20871215252208009 and -4.7912878474779195
Page | 1
JAVA PROGRAMS FOR PRACTICE
13. Write a Java program to classify Abundant, deficient and perfect number (integers)
between 1 to 10,000.
In number theory, an abundant number is a number for which the sum of its proper divisors
is greater than the number itself.
Example :
The first few abundant numbers are:
12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100, 102,…
The integer 12 is the first abundant number. Its proper divisors are 1, 2, 3, 4 and 6 for a total
of 16.
Deficient number: In number theory, a deficient number is a number n for which the sum of
divisors σ(n)<2n. The value 2n − σ(n) is called the number's deficiency.
As an example, divisors of 21 are 1, 3 and 7, and their sum is 11. Because 11 is less than
21, the number 21 is deficient. Its deficiency is 2 × 21 − 11 = 31.
The first few deficient numbers are:
1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19, 21, 22, 23, 25, 26, 27, 29, 31, 32, 33,
…….
Perfect number: In number system, a perfect number is a positive integer that is equal to the
sum of its proper positive divisors, that is, the sum of its positive divisors excluding the
number itself.
The first perfect number is 6. Its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6.
The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers
496 and 8128.
Expected Output:
Number Counting [(integers) between 1 to 10,000]:
Deficient number: 7508
Perfect number: 4
Abundant number: 2488
14. Write a Java program to find and print the first 50 happy numbers.
Happy number: Starting with any positive integer, replace the number by the sum of the
squares of its digits, and repeat the process until the number equals 1, or it loops endlessly
in a cycle which does not include 1.
Example: 19 is a happy number
12 + 92=82
Page | 2
JAVA PROGRAMS FOR PRACTICE
82 + 22=68
62 + 82=100
12 + 02 + 02=1
Expected Output
Happy numbers are:
1
7
10
13
19
23
28
31
15. Write a Java program to check whether a given number is a Disarium number or
unhappy number.
A Disarium number is a number defined by the following process :
Sum of its digits powered with their respective position is equal to the original number.
For example 135 is a Disarium number :
As 11+32+53 = 135
Some other DISARIUM are 89, 175, 518 etc.
A number will be called Disarium if the sum of its digits powered with their respective
position is equal with the number itself. Sample Input: 135.
Expected Output:
Input a number: 25
Not a Disarium Number.
Page | 3