Basic Java PracticeCode Set 2
Basic Java PracticeCode Set 2
Write a Java program to print 'Hello' on screen and then print your name on a
separate line.
Expected Output :
Hello
Alexandra Abramov
3. Write a Java program to divide two numbers and print on the screen.
Test Data :
50/3
Expected Output :
16
5. Write a Java program that takes two numbers as input and display the product of
two numbers.
Test Data:
Input first number: 25
Input second number: 5
Expected Output :
25 x 5 = 125
6. Write a Java program to print the sum (addition), multiply, subtract, divide and
remainder of two numbers.
Test Data:
Input first number: 125
Input second number: 24
Expected Output :
125 + 24 = 149
125 - 24 = 101
125 x 24 = 3000
125 / 24 = 5
125 mod 24 = 5
7. Write a Java program that takes a number as input and prints its multiplication
table upto 10.
Test Data:
Input a number: 8
Expected Output :
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
...
8 x 10 = 80
J a v v a
J a a v v a a
J J aaaaa V V aaaaa
JJ a a V a a
9. Write a Java program to compute the specified expressions and print the output.
Test Data:
((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5))
Expected Output
2.138888888888889
11. Write a Java program to print the area and perimeter of a circle.
Test Data:
Radius = 7.5
Expected Output
Perimeter is = 47.12388980384689
Area is = 176.71458676442586
12. Write a Java program that takes three numbers as input to calculate and print
the average of the numbers.
13. Write a Java program to print the area and perimeter of a rectangle.
Test Data:
Width = 5.5 Height = 8.5
Expected Output
Area is 5.6 * 8.5 = 47.60
Perimeter is 2 * (5.6 + 8.5) = 28.20
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
==============================================
==============================================
==============================================
==============================================
==============================================
==============================================
+"""""+
[| o o |]
| ^ |
| '-' |
+-----+
Hexadecimal number is : F
============================================================================
Decimal Number: 4
HexaDecimal value: D
Octal number: 7
31. Write a Java program to check whether Java is installed on your computer.
Expected Output
25 != 39
25 < 39
25 <= 39
33. Write a Java program and compute the sum of the digits of an integer.
Input Data:
Input an integer: 25
Expected Output
36. Write a Java program to compute the distance between two points on the surface
of earth.
Distance between the two points [ (x1,y1) & (x2,y2)]
d = radius * arccos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2))
Radius of the earth r = 6371.01 Kilometers
Input Data:
Input the latitude of coordinate 1: 25
Input the longitude of coordinate 1: 35
Input the latitude of coordinate 2: 35.5
Input the longitude of coordinate 2: 25.5
Expected Output
38. Write a Java program to count the letters, spaces, numbers and other characters
of an input string.
Expected Output
39. Write a Java program to create and display unique three-digit number using 1,
2, 3, 4. Also count how many three-digit numbers are there.
Expected Output
123
124
...
431
432
Total number of the three-digit-number is 24
40. Write a Java program to list the available character sets in charset objects.
Expected Output
...
x-SJIS_0213
x-UTF-16LE-BOM
X-UTF-32BE-BOM
X-UTF-32LE-BOM
x-windows-50220
x-windows-50221
x-windows-874
x-windows-949
x-windows-950
x-windows-iso2022jp
41. Write a Java program to print the ascii value of a given character.
Expected Output
43. Write a Java program to print the following string in a specific format (see
the output).
Sample Output
44. Write a Java program that accepts an integer (n) and computes the value of
n+nn+nnn.
Sample Output:
Input number: 5
5 + 55 + 555
/home/students/abc.txt : 0 bytes
/home/students/test.txt : 0 bytes
47. Write a Java program to display the current date time in specific format.
Sample Output:
48. Write a Java program to print the odd numbers from 1 to 99. Prints one number
per line.
Sample Output:
1
3
5
7
9
11
....
91
93
95
97
99
49. Write a Java program to accept a number and check the number is even or not.
Prints 1 if the number is even or 0 if the number is odd.
Sample Output:
Input a number: 20
1
50. Write a Java program to print numbers between 1 to 100 which are divisible by
3, 5 and by both.
Sample Output:
Divided by 3:
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57
, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99,
Divided by 5:
5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90,
95,
Divided by 3 & 5:
15, 30, 45, 60, 75, 90,
Input a number(string): 25
The integer value is: 25
52. Write a Java program to calculate the sum of two integers and return true if
the sum is equal to a third integer.
Sample Output:
53. Write a Java program that accepts three integers from the user and return true
if the second number is greater than first number and third number is greater than
second number. If "abc" is true second number does not need to be greater than
first number.
Sample Output:
54. Write a Java program that accepts three integers from the user and return true
if two or more of them (integers ) have the same rightmost digit. The integers are
non-negative.
Sample Output:
55. Write a Java program to convert seconds to hour, minute and seconds.
Sample Output:
56. Write a Java program to find the number of integers within the range of two
specified numbers and that are divisible by another number.
For example x = 5, y=20 and p =3, find the number of integers within the range x..y
and that are divisible by p i.e. { i :x ≤ i ≤ y, i mod p = 0 }
Sample Output:
57. Write a Java program to accepts an integer and count the factors of the number.
Sample Output:
Input an integer: 25
3
58. Write a Java program to capitalize the first letter of each word in a sentence.
Sample Output:
Input a Sentence: the quick brown fox jumps over the lazy dog.
The Quick Brown Fox Jumps Over The Lazy Dog.
Input a String: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
the quick brown fox jumps over the lazy dog.
60. Write a Java program to find the penultimate (next to last) word of a sentence.
Sample Output:
Input a String: The quick brown fox jumps over the lazy dog.
Penultimate word: lazy
62. Write a Java program that accepts three integer values and return true if one
of them is 20 or more and less than the substractions of others.
Sample Output:
64. Write a Java program that accepts two integer values between 25 to 75 and
return true if there is a common digit in both numbers.
Sample Output:
65. Write a Java program to calculate the modules of two numbers without using any
inbuilt modulus operator.
Sample Output:
66. Write a Java program to compute the sum of the first 100 prime numbers.
Sample Output:
67. Write a Java program to insert a word in the middle of the another string.
Insert "Tutorial" in the middle of "Python 3.0", so result will be Python Tutorial
3.0
Sample Output:
68. Write a Java program to create a new string of 4 copies of the last 3
characters of the original string. The length of the original string must be 3 and
above.
Sample Output:
3.03.03.03.0
69. Write a Java program to extract the first half of a string of even length.
Test Data: Python
Sample Output:
Pyt
70. Write a Java program to create a string in the form short_string + long_string
+ short_string from two strings. The strings must not have the same length.
Test Data: Str1 = Python
Str2 = Tutorial
Sample Output:
PythonTutorialPython
71. Write a Java program to create the concatenation of the two strings except
removing the first character of each string. The length of the strings must be 1
and above.
Test Data: Str1 = Python
Str2 = Tutorial
Sample Output:
ythonutorial
72. Write a Java program to create a new string taking first three characters from
a given string. If the length of the given string is less than 3 use "#" as
substitute characters.
Test Data: Str1 = " "
Sample Output:
###
73. Write a Java program to create a new string taking first and last characters
from two given strings. If the length of either string is 0 use "#" for missing
character.
Test Data: str1 = "Python"
str2 = " "
Sample Output:
P#
74. Write a Java program to test if 10 appears as either the first or last element
of an array of integers. The length of the array must be greater than or equal to
2.
Sample Output:
Test Data: array = 10, -20, 0, 30, 40, 60, 10
true
75. Write a Java program to test if the first and the last element of an array of
integers are same. The length of the array must be greater than or equal to 2.
Test Data: array = 50, -20, 0, 30, 40, 60, 10
Sample Output:
false
76. Write a Java program to test if the first and the last element of two array of
integers are same. The length of the array must be greater than or equal to 2.
Test Data: array1 = 50, -20, 0, 30, 40, 60, 12
array2 = 45, 20, 10, 20, 30, 50, 11
Sample Output:
false
77. Write a Java program to create a new array of length 2 from two arrays of
integers with three elements and the new array will contain the first and last
elements from the two arrays.
Test Data: array1 = 50, -20, 0
array2 = 5, -50, 10
Sample Output:
80. Write a Java program to get the larger value between first and last element of
an array (length 3) of integers .
Sample Output:
81. Write a Java program to swap the first and last elements of an array (length
must be at least 1) and create a new array.
Sample Output:
82. Write a Java program to find the largest element between first, last, and
middle values from an array of integers (even length).
Sample Output:
Result: 1 12 25 -8
84. Write a Java program to take the last three characters from a given string and
add the three characters at both the front and back of the string. String length
must be greater than three and more.
Test data: "Python" will be "honPythonhon"
Sample Output:
honPythonhon
85. Write a Java program to check if a string starts with a specified word.
Sample Data: string1 = "Hello how are you?"
Sample Output:
true
86. Write a Java program start with an integer n, divide n by 2 if n is even or
multiply by 3 and add 1 if n is odd, repeat the process until n = 1.
87. Write a Java program than read an integer and calculate the sum of its digits
and write the number of each digit of the sum in English.
88. Write a Java program to get the current system environment and system
properties.
89. Write a Java program to check whether a security manager has already been
established for the current application or not.
90. Write a Java program to get the value of the environment variable PATH, TEMP,
USERNAME.
91. Write a Java program to measure how long some code takes to execute in
nanoseconds.
92. Write a Java program to count the number of even and odd elements in a given
array of integers.
94. Write a Java program to rearrange all the elements of an given array of
integers so that all the odd numbers come before all the even numbers.
95. Write a Java program to create an array (length # 0) of string values. The
elements will contain "0", "1", "2" … through ... n-1.
98. Write a Java program to check if the value 20 appears three times and no 20's
are next to each other in an given array of integers.
99. Write a Java program to check if a specified number appears in every pair of
adjacent element of a given array of integers.
100. Write a Java program to count the two elements differ by 1 or less of two
given arrays of integers with same length.
101. Write a Java program to check if the number of 10 is greater than number to
20's in a given array of integers.
102. Write a Java program to check if a specified array of integers contains 10's
or 30's.
103. Write a Java program to create a new array from a given array of integers, new
array will contain the elements from the given array after the last element value
10.
104. Write a Java program to create a new array from a given array of integers, new
array will contain the elements from the given array before the last element value
10.
105. Write a Java program to check if a group of numbers (l) at the start and end
of a given array are same.
106. Write a Java program to create a new array that is left shifted from a given
array of integers.
108. Write a Java program to add all the digits of a given positive integer until
the result has a single digit.
109. Write a Java program to form a staircase shape of n coins where every k-th row
must have exactly k coins.
110. Write a Java program to check whether an given integer is a power of 4 or not.
Given num = 64, return true. Given num = 6, return false.
111. Write a Java program to add two numbers without using any arithmetic
operators.
Given x = 10 and y = 12; result = 22
112. Write a Java program to compute the number of trailing zeros in a factorial.
7! = 5040, therefore the output should be 1
113. Write a Java program to merge two given sorted array of integers and create a
new sorted array.
array1 = [1,2,3,4]
array2 = [2,5,7, 8]
result = [1,2,2,3,4,5,7,8]
114. Write a Java program to given a string and an offset, rotate string by offset
(rotate from left to right).
115. Write a Java program to check if a positive number is a palindrome or not.
Input a positive integer: 151
Is 151 is a palindrome number?
true
116. Write a Java program which iterates the integers from 1 to 100. For multiples
of three print "Fizz" instead of the number and print "Buzz" for the multiples of
five. When number is divided by both three and five, print "fizz buzz".
117. Write a Java program to compute the square root of an given integer.
Input a positive integer: 25
Square root of 25 is: 5
118. Write a Java program to get the first occurrence (Position starts from 0.) of
a string within a given string.
119. Write a Java program to get the first occurrence (Position starts from 0.) of
an element of a given array.
122. Write a Java program to find a contiguous subarray with largest sum from a
given array of integers.
Note: In computer science, the maximum subarray problem is the task of finding the
contiguous subarray within a one-dimensional array of numbers which has the largest
sum. For example, for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4; the
contiguous subarray with the largest sum is 4, −1, 2, 1, with sum 6. The subarray
should contain one integer at least.
123. Write a Java program to find the subarray with smallest sum from a given array
of integers.
124. Write a Java program to find the index of a value in a sorted array. If the
value does not find return the index where it would be if it were inserted in
order.
Example:
[1, 2, 4, 5, 6] 5(target) -> 3(index)
[1, 2, 4, 5, 6] 0(target) -> 0(index)
[1, 2, 4, 5, 6] 7(target) -> 5(index)
125. Write a Java program to get the preorder traversal of its nodes' values of a
given a binary tree.
Example:
10
/ \
20 30
/ \
40 50
Expected output: 10 20 40 50 30
126. Write a Java program to get the inorder traversal of its nodes' values of a
given a binary tree.
10
/ \
20 30
/ \
40 50
Example:{10, 20, 30, 40, 50}
Output: 40 20 50 10 30
127. Write a Java program to get the Postorder traversal of its nodes' values of a
given a binary tree.
10
/ \
20 30
/ \
40 50
129. Write a Java program to find a number that appears only once in a given array
of integers, all numbers occur twice.
Source Array : [10, 20, 10, 20, 30, 40, 40, 30, 50] 50 appears only once
130. Write a Java program to find the maximum depth of a given a binary tree.
Sample Output: The Maximum depth of the binary tree is: 3
131. Write a Java program to find the new length of a given sorted array where each
element appear only once (remove the duplicates ).
Original array: [1, 1, 2, 3, 3, 3, 4, 5, 6, 7, 7] The length of the original array
is: 11 After removing duplicates, the new length of the array is: 7
132. Write a Java program to find the new length of a given sorted array where
duplicate elements appeared at most twice.
Original array: [1, 1, 2, 3, 3, 3, 4, 5, 6, 7, 7, 7, 7]
The length of the original array is: 13
After removing duplicates, the new length of the array is: 10
133. Write a Java program to find a path from top left to bottom in right direction
which minimizes the sum of all numbers along its path.
Note: Move either down or right at any point in time.
Sample Output: Sum of all numbers along its path: 13
134. Write a Java program to find the distinct ways you can climb to the top (n
steps to reach to the top) of stairs. Each time you can either climb 1 or 2 steps.
Example: n = 5
a) 1+1+1+1+1 = 5 b) 1+1+1+2 = 5 c) 1+2+2 = 5 d) 2+2+1 = 5 e) 2+1+1+1 = 5 f) 2+1+2 =
5 g) 1+2+1+1 = 5 h) 1+1+2+1 = 5
Sample Output: Distinct ways can you climb to the top: 8
135. Write a Java program to remove duplicates from a sorted linked list.
Original List with duplicate elements:
12->12->13->14->15->15->16->17->17
After removing duplicates from the said list:
12->13->14->15->16->17
136. Write a Java program to find possible unique paths from top-left corner to
bottom-right corner of a given grid (m x n).
Note: You can move either down or right at any point in time.
Sample Output: Unique paths from top-left corner to bottom-right corner of the said
grid: 3
137. Write a Java program to find possible unique paths considering some obstacles,
from top-left corner to bottom-right corner of a given grid (m x n).
Note: You can move either down or right at any point in time and an obstacle and
empty space is marked as 1 and 0 respectively in the grid.
Sample grid:
int[][] obstacle_Grid ={
{0, 0, 0},
{0, 1, 0},
{0, 0, 0},
};
Sample Output: Unique paths from top-left corner to bottom-right corner of the said
grid (considering some obstacles): 2
138. Write a Java program to find all of the longest word in a given dictionary.
Example-1:
{
"cat",
"flag",
"green",
"country",
"w3resource"
}
Result: "w3resource"
Example-2:
{
"cat",
"dog",
"red",
"is",
"am"
}
Result: "cat", "dog", "red"
139. Write a Java program to get the index of the first number and the last number
of a subarray where the sum of numbers is zero from a given array of integers.
Original Array : [1, 2, 3, -6, 5, 4]
Index of the subarray of the said array where the sum of numbers is zero: [0, 3]
140. Write a Java program to merge all overlapping Intervals from a given a
collection of intervals.
Sample Output: 1 6
8 10
15 20
141. Write a Java program to check if a given string has all unique characters.
Sample Output: Original String : xyyz
String has all unique characters: false
142. Write a Java program to check if two given strings are anagrams or not.
According to Wikipedia "An anagram is a word or phrase formed by rearranging the
letters of a different word or phrase, typically using all the original letters
exactly once. For example, the word anagram can be rearranged into nag a ram, or
the word binary into brainy."
Sample Output: String-1 : wxyz
String-2 : zyxw
Check if two given strings are anagrams or not?: true
144. Write a Java program to remove all occurrences of a specified value in a given
array of integers and return the new length of the array.
Sample Output:
Original array: [1, 4, 6, 7, 6, 2]
The length of the new array is: 4
145. Write a Java program to remove the nth element from the end of a given list.
Sample Output:
Original node:
1 2 3 4 5
After removing 2nd element from end:
1 2 3 5
146. Write a Java program to convert an sorted array to binary search tree.
Maintain minimal height of the tree.
Sample Output:
2
1
4
6
5
3
147. Write a Java program to find the number of bits required to flip to convert
two given integers.
Sample Output:
2
148. Write a Java program to find the index of the first unique character in a
given string, assume that there is at least one unique character in the string.
Sample Output:
Original String: wresource
First unique character of the above: 0
150. Write a Java program to test if a binary tree is a subtree of another binary
tree.
Sample Output:
Original strings: xxyz yxzx
true
Expected Output :
(101 + 0) / 3)-> 33
(3.0e-6 * 10000000.1)-> 30.0000003
(true && true)-> true
(false && true)-> false
((false && false) || (true && true))-> true
(false || false) && (true && true)-> false
152. Write a Java program that accepts four integer from the user and prints equal
if all four are equal, and not equal otherwise.
Sample Output:
Input first number: 25
Input second number: 37
Input third number: 45
Input fourth number: 23
Numbers are not equal!
153. Write a Java program that accepts two double variables and test if both
strictly between 0 and 1 and false otherwise.
Sample Output:
Input first number: 5
Input second number: 1
false
154. Write a Java program to print the contents of a two-dimensional Boolean array
where t will represent true and f will represent false.
Sample array:
array = {{true, false, true},
{false, true, false}};
Expected Output :
t f t
f t f
155. Write a Java program to print an array after changing the rows and columns of
a given two-dimensional array.
Original Array:
10 20 30
40 50 60
After changing the rows and columns of the said array:10 40
20 50
30 60
156. Write a Java program that returns the largest integer but not larger than the
base-2 logarithm of a specified integer.
157. Write a Java program to prove that Euclid’s algorithm computes the greatest
common divisor of two positive given integers.
According to Wikipedia "The Euclidean algorithm is based on the principle that the
greatest common divisor of two numbers does not change if the larger number is
replaced by its difference with the smaller number. For example, 21 is the GCD of
252 and 105 (as 252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the
GCD of 105 and 252 − 105 = 147. Since this replacement reduces the larger of the
two numbers, repeating this process gives successively smaller pairs of numbers
until the two numbers become equal. When that occurs, they are the GCD of the
original two numbers. By reversing the steps, the GCD can be expressed as a sum of
the two original numbers each multiplied by a positive or negative integer, e.g.,
21 = 5 × 105 + (−2) × 252. The fact that the GCD can always be expressed in this
way is known as Bézout's identity."
Expected Output:
result: 24
result: 1
158. Write a Java program to create a two-dimension array (m x m) A[][] such that
A[i][j] is true if I and j are prime and have no common factors, otherwise A[i][j]
becomes false.
Sample Output:
true true true
true true true
true true false
159. Write a Java program to find the k largest elements in a given array. Elements
in the array can be in any order.
Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
3 largest elements of the said array are:
100 25 17
160. Write a Java program to find the k smallest elements in a given array.
Elements in the array can be in any order.
Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
3 largest elements of the said array are:
100 25 17
161. Write a Java program to find the kth smallest and largest element in a given
array. Elements in the array can be in any order.
Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
K'th smallest element of the said array:
3
K'th largest element of the said array:
25
162. Write a Java program to find the numbers greater than the average of the
numbers of a given array.
Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
The average of the said array is: 22.0
The numbers in the said array that are greater than the average are:
25
100
163. Write a Java program that will accept an integer and convert it into a binary
representation. Now count the number of bits which is equal to zero of the said
binary representation.
Expected Output:
Input first number: 25
Binary representation of 25 is: 11001
Number of zero bits: 2
164. Write a Java program to divide the two given integers using subtraction
operator.
Expected Output:
Input the dividend: 625
Input the divider: 25
Result: 25.0
165. Write a Java program to move every positive number to the right and every
negative number to the left of a given array of integers.
Expected Output:
Original array: [-2, 3, 4, -1, -3, 1, 2, -4, 0]
Result: [-4, -3, -2, -1, 0, 1, 2, 3, 4]
Expected Output:
Input an integer: 35
String format of the said integer: 35
167. Write a Java program to move every zero to the right side of a given array of
integers.
168. Write a Java program to multiply two given integers without using the multiply
operator(*).
Input a string: The quick brown fox jumps over the lazy dog
Result: dog lazy the over jumps fox brown quick The
170. Write a Java program to find the length of the longest consecutive sequence of
a given array of integers.
171. Write a Java program to accept two string and test if the second string
contains the first one.
172. Write a Java program to get the number of element in a given array of integers
that are smaller than the integer of another given array of integers.
Expected Output:
0
3
7
173. Write a Java program to find the median of the number inside the window (size
k) at each moving in a given array of integers with duplicate numbers. Move the
window from the start of the array.
Sample Output:
{|1, 2, 3|, 4, 5, 6, 7, 8, 8} -> Return median 2
{1, |2, 3, 4|, 5, 6, 7, 8, 8} -> Return median 3
{1, 2, |3, 4, 5|, 6, 7, 8, 8} -> Return median 4
{1, 2, 3, |4, 5, 6|, 7, 8, 8} -> Return median 5
{1, 2, 3, 4, |5, 6, 7|, 8, 8} -> Return median 6
{1, 2, 3, 4, 5, |6, 7, 8|, 8} -> Return median 7
{1, 2, 3, 4, 5, 6, |7, 8, 8|} -> Return median 8
Result array {2, 3, 4, 5, 6, 7, 8}
Expected Output:
Value of k: 3
Result:
2
3
4
5
6
7
8
174. Write a Java program to find the maximum number inside the number in the
window (size k) at each moving in a given array of integers with duplicate numbers.
Move the window from the start of the array.
Sample output:
{|1, 2, 3|, 4, 5, 6, 7, 8, 8} -> Return maximum 3
{1, |2, 3, 4|, 5, 6, 7, 8, 8} -> Return maximum 4
{1, 2, |3, 4, 5|, 6, 7, 8, 8} -> Return maximum 5
{1, 2, 3, |4, 5, 6|, 7, 8, 8} -> Return maximum 6
{1, 2, 3, 4, |5, 6, 7|, 8, 8} -> Return maximum 7
{1, 2, 3, 4, 5, |6, 7, 8|, 8} -> Return maximum 8
{1, 2, 3, 4, 5, 6, |7, 8, 8|} -> Return maximum 8
Result array {3, 4, 5, 6, 7, 8, 8}
Expected Output:
Value of k: 3
Result:
2
3
4
5
6
7
8
175. Write a Java program to delete a specified node in the middle of a singly
linked list.
176. Write a Java program to partition an given array of integers into even number
first and odd number second.
Expected Output
Expected Output:
Original Treenode:
4
5
2
3
1
178. Write a Java program to find the longest increasing continuous subsequence in
a given array of integers.
Expected Output:
179. Write a Java program to plus one to the number of a given positive numbers
represented as an array of digits.
Expected Output:
180. Write a Java program to swap every two adjacent nodes of a given linked list.
Expected Output:
181. Write a Java program to find the length of last word of a given string. The
string contains upper/lower-case alphabets and empty space characters ' '.
Sample Output:
Sample Output:
183. Write a Java program to accept a positive number and repeatedly add all its
digits until the result has only one digit.
Expected Output:
184. Write a Java program to find the length of the longest consecutive sequence
path of a given binary tree.
Note: The longest consecutive path need to be from parent to child.
Java Basic Exercises: Find the length of the longest consecutive sequence path of a
given binary tree.
Expected Output:
185. Write a Java program to check if two given strings are isomorphic or not.
Expected Output:
Expected Output:
Expected Output:
188. Write a Java program to find all the start indices of a given string's
anagrams in another given string.
Expected Output:
189. Write a Java program to Given two non-negative integers num1 and num2
represented as string, return the sum of num1 and num2.
Expected Output:
190. Write a Java program to find the missing string from two given strings.
Expected Output:
191. Write a Java program to test whether there are two integers x and y such that
x^2 + y^2 is equal to a given positive number.
Expected Output:
192. Write a Java program to rearrange the alphabets in the order followed by the
sum of digits in a given string containing uppercase alphabets and integer digits
(from 0 to 9).
Expected Output:
ADEHNS23
193. Write a Java program that accept an integer and find the sum of all the
elements from all possible subsets of a set formed by first n natural numbers.
Expected Output:
Expected Output:
(0,2)
(1,0)
(2,1)
195. Write a Java program to check if three given side lengths (integers) can make
a triangle or not.
Expected Output:
Input side1: 5
Input side2: 6
Input side3: 8
Is the said sides form a triangle: true
196. rite a Java program to create a spiral array of n * n sizes from a given
integer n.
Expected Output:
Input a number: 5
Spiral array becomes:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Expected Output:
198. Write a Java program to get the position of a given prime number.
Expected Output:
Example pattern:
Given pattern = "xyyx", str = "red black black red", return true.
Given pattern = "xyyx", str = "red black black green", return false.
Given pattern = "xxxx", str = "red black black red", return false.
Given pattern = "xxxx", str = "red red red red", return true.
Expected Output:
Expected Output:
201. Write a Java program to divide a given array of integers into given k non-
empty subsets whose sums are all equal. Return true if all sums are equal otherwise
return false.
Example:
nums = {1,3,3,5,6,6}, k = 4;
4 subsets (5,1), (3, 3), (6), (6) with equal sums.
Expected Output:
202. Write a Java program to find the total number of continuous subarrays in a
given array of integers whose sum equals to an given integer.
Expected Output:
203. Write a Java program to find the contiguous subarray of given length k which
has the maximum average value of a given array of integers. Display the maximum
average value.
Expected Output:
Expected Output:
Input x : 25
Input n : 35
Input y : 45
x^n % y = 5.0
205. Write a Java program to check whether an given integer is power of 2 or not
using O(1) time.
Note: O(1) means that it takes a constant time, like 12 nanoseconds, or two minutes
no matter the amount of data in the set.
O(n) means it takes an amount of time linear with the size of the set, so a set
twice the size will take twice the time. You probably don't want to put a million
objects into one of these.
Expected Output:
Input a number : 25
false
206. Write a Java program to find all unique combinations from a collection of
candidate numbers. The sum of the numbers will be equal to a given target number.
Expected Output:
207. Write a Java program to merge two sorted (ascending) linked lists in ascending
order.
Expected Output:
Merged list: 1 2 3 4 5 6
208. Write a Java program to create a basic string compression method using the
counts of repeated characters.
Input string: aaaabbbbcccccddddeeee
Expected Output:
209. Write a Java program to find all unique combinations from a collection of
candidate numbers. The sum of the numbers will be equal to a given target number.
Expected Output:
Enter elements:
6 7 8
Enter target sum:
21
A solution set is:
{ 6 7 8 }
210. Write a Java program to match any single character (use ?) or any sequence of
characters use *) including the empty. The matching should cover the entire input
string.
Expected Output:
Enter a string
bb
Enter a pattern
b*
Yes
211. Write a Java program to find heights of the top three building in descending
order from eight given buildings.
Input:
Expected Output:
Input:
Each test case consists of two non-negative integers a and b which are separated by
a space in a line. 0 ≤ a, b ≤ 1,000,000
Expected Output:
213. Write a Java program to check whether three given lengths (integers) of three
sides form a right triangle. Print "Yes" if the given sides form a right triangle
otherwise print "No".
Input:
Each test case consists of two non-negative integers a and b which are separated by
a space in a line. 0 ≤ a, b ≤ 1,000,000
Expected Output:
Input:
Expected Output:
215. Write a Java program to compute the amount of the debt in n months. The
borrowing amount is $100,000 and the loan adds 4% interest of the debt and rounds
it to the nearest 1,000 above month by month.
Input:
An integer n (0 ≤ n ≤ 100)
Expected Output:
Input number of months:
6
Amount of debt:
129000
216. Write a Java program which reads an integer n and find the number of
combinations of a,b,c and d (0 ≤ a,b,c,d ≤ 9) where (a + b + c + d) will be equal
to n.
Input:
Expected Output:
217. Write a Java program to print the number of prime numbers which are less than
or equal to a given integer.
Input:
n (1 ≤ n ≤ 999,999)
Expected Output:
218. Write a Java program to compute the radius and the central coordinate (x, y)
of a circle which is constructed by three given points on the plane surface.
Input:
Expected Output:
219. Write a Java program to check if a point (x, y) is in a triangle or not. There
is a triangle formed by three points.
Input:
x1, y1, x2, y2, x3, y3 separated by a single space.
Expected Output:
220. Write a Java program to compute and print sum of two given integers (more than
or equal to zero). If given integers or the sum have more than 80 digits, print
"overflow".
Input:
Expected Output:
221. Write a Java program that accepts six numbers as input and sorts them in
descending order.
Input:
Input consists of six numbers n1, n2, n3, n4, n5, n6 (-100000 ≤ n1, n2, n3, n4, n5,
n6 ≤ 100000). The six numbers are separated by a space.
Expected Output:
222. Write a Java program to test whether two lines PQ and RS are parallel. The
four points are P(x1, y1), Q(x2, y2), R(x3, y3), S(x4, y4).
Input:
223. Write a Java program to find the maximum sum of a contiguous subsequence from
a given sequence of numbers a1, a2, a3, ... an. A subsequence of one element is
also a continuous subsequence.
Input:
Expected Output:
224. There are two circles C1 with radius r1, central coordinate (x1, y1) and C2
with radius r2 and central coordinate (x2, y2)
Write a Java program to test the followings -
"C2 is in C1" if C2 is in C1
"C1 is in C2" if C1 is in C2
"Circumference of C1 and C2 intersect" if circumference of C1 and C2 intersect, and
"C1 and C2 do not overlap" if C1 and C2 do not overlap.
Input:
Expected Output:
225. Write a Java program to that reads a date (from 2004/1/1 to 2004/12/31) and
prints the day of the date. Jan. 1, 2004, is Thrusday. Note that 2004 is a leap
year.
Expected Output:
226. Write a Java program to print mode values from a given a sequence of integers.
The mode value is the element which occurs most frequently. If there are several
mode values, print them in ascending order.
Input:
Expected Output:
227. Write a Java program which reads a text (only alphabetical characters and
spaces.) and prints two words. The first one is the word which is arise most
frequently in the text. The second one is the word which has the maximum number of
letters.
Note: A word is a sequence of letters which is separated by the spaces.
Input:
Expected Output:
228. Write a Java program that reads n digits (given) chosen from 0 to 9 and prints
the number of combinations where the sum of the digits equals to another given
number (s). Do not use the same digits in a combination.
For example, the combinations where n = 3 and s = 6 are as follows:
1 + 2 + 3 = 6
0 + 1 + 5 = 6
0 + 2 + 4 = 6
Input:
Two integers as number of combinations and their sum by a single space in a line.
Input 0 0 to exit.
Expected Output:
229. Write a Java program which reads the two adjoined sides and the diagonal of a
parallelogram and check whether the parallelogram is a rectangle or a rhombus.
According to Wikipedia-
parallelograms: In Euclidean geometry, a parallelogram is a simple (non-self-
intersecting) quadrilateral with two pairs of parallel sides. The opposite or
facing sides of a parallelogram are of equal length and the opposite angles of a
parallelogram are of equal measure.
rectangles: In Euclidean plane geometry, a rectangle is a quadrilateral with four
right angles. It can also be defined as an equiangular quadrilateral, since
equiangular means that all of its angles are equal (360°/4 = 90°). It can also be
defined as a parallelogram containing a right angle.
rhombus: In plane Euclidean geometry, a rhombus (plural rhombi or rhombuses) is a
simple (non-self-intersecting) quadrilateral whose four sides all have the same
length. Another name is equilateral quadrilateral, since equilateral means that all
of its sides are equal in length. The rhombus is often called a diamond, after the
diamonds suit in playing cards which resembles the projection of an octahedral
diamond, or a lozenge, though the former sometimes refers specifically to a rhombus
with a 60° angle (see Polyiamond), and the latter sometimes refers specifically to
a rhombus with a 45° angle.
Input:
Expected Output:
Input two adjoined sides and the diagonal of a parallelogram (comma separated):
8,8,8
This is a rhombus.
230. Write a Java program to replace a string "python" with "java" and "java" with
"python" in a given string.
Input:
231. Write a Java program to find the difference between the largest integer and
the smallest integer which are created by 8 numbers from 0 to 9. The number that
can be rearranged shall start with 0 as in 00135668.
Input:
Sample Output:
232. Write a Java program to compute the sum of first n given prime numbers.
Input:
Sample Output:
233. Write a Java program that accept a even number (n should be greater than or
equal to 4 and less than or equal to 50,000, Goldbach number) from the user and
create a combinations that express the given number as a sum of two prime numbers.
Print the number of combinations.
Goldbach number: A Goldbach number is a positive even integer that can be expressed
as the sum of two odd primes.[4] Since four is the only even number greater than
two that requires the even prime 2 in order to be written as the sum of two primes,
another form of the statement of Goldbach's conjecture is that all even integers
greater than 4 are Goldbach numbers.
The expression of a given even number as a sum of two primes is called a Goldbach
partition of that number. The following are examples of Goldbach partitions for
some even numbers:
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7 = 5 + 5
12 = 7 + 5
...
100 = 3 + 97 = 11 + 89 = 17 + 83 = 29 + 71 = 41 + 59 = 47 + 53
Sample Output:
Number of combinations: 6
234. If you draw a straight line on a plane, the plane is divided into two regions.
For example, if you pull two straight lines in parallel, you get three areas, and
if you draw vertically one to the other you get 4 areas.
Sample Output:
235. There are four different points on a plane, P(xp,yp), Q(xq, yq), R(xr, yr) and
S(xs, ys).
Write a Java program to test whether AB and CD are orthogonal or not.
Write a Java program to create maximum number of regions obtained by drawing n
given straight lines.
Input:
xp,yp, xq, yq, xr, yr, xs and ys are -100 to 100 respectively and each value can be
up to 5 digits after the decimal point It is given as a real number including the
number of.
Output: Yes or No.
Sample Output:
236. Write a Java program to sum of all numerical values (positive integers)
embedded in a sentence.
Input:
Sentences with positive integers are given over multiple lines. Each line is a
character string containing one-byte alphanumeric characters, symbols, spaces, or
an empty line. However the input is 80 characters or less per line and the sum is
10,000 or less.
Sample Output:
237. There are 10 vertical and horizontal squares on a plane. Each square is
painted blue and green. Blue represents the sea, and green represents the land.
When two green squares are in contact with the top and bottom, or right and left,
they are said to be ground. The area created by only one green square is called
"island". For example, there are five islands in the figure below.
Write a Java program to read the mass data and find the number of islands.
Input:
A single data set is represented by 10 rows of 10 numbers representing green
squares as 1 and blue squares as zeros.
Output: For each data set, output the number of islands.
Sample Output:
Input:
Multiple character strings are given. One string is given per line.
Output: The restored character string for each character on one line.
Sample Output:
239. Internet search engine giant, such as Google accepts web pages around the
world and classify them, creating a huge database. The search engines also analyze
the search keywords entered by the user and create inquiries for database search.
In both cases, complicated processing is carried out in order to realize efficient
retrieval, but basics are all cutting out words from sentences.
Write a Java program to cut out words of 3 to 6 characters length from a given
sentence not more than 1024 characters.
Input:
English sentences consisting of delimiters and alphanumeric characters are given on
one line.
Output: Output a word delimited by one space character on one line.
Sample Output:
Input:
A series of integers separated by commas are given in diamonds. No spaces are
included in each line. The input example corresponds to Figure 1. The number of
lines of data is less than 100 lines.
Output: The maximum value of the sum of integers passing according to the rule on
one line.
Sample Output:
241. Write a Java program to find the number of combinations that satisfy p + q + r
+ s = n where n is a given number <= 4000 and p, q, r, s in the range of 0 to 1000.
Sample Output:
243. Write a Java program which reads a list of pairs of a word and a page number,
and prints the word and a list of the corresponding page numbers.
Input:
Input pairs of a word and a page number:
apple 5
banana 6
244. Write a Java program which accepts a string from the user and check whether
the string is correct or not.
Input:
Input a string:
XYZ
Correct format..
245. Write a Java program which accepts students name, id, and marks and display
the highest score and the lowest score.
The student name and id are all strings of no more than 10 characters. The score is
an integer between 0 and 100.
Input:
Input number of students:
3
Input Student Name, ID, Score:
Devid v1 72
Peter v2 68
Johnson v3 85
name, ID of the highest score and the lowest score:
Johnson v3
Peter v2
246. Let us use the letter H to mean "hundred", the letter T to mean "ten" and “1,
2, . . . n” to represent the ones digit n (<10). Write a Java program to convert 3
digits positive number in given format. For example, 234 should be output as
BBSSS1234 because it has 2 "hundreds", 3 "ten", and 4 of the ones.
The student name and id are all strings of no more than 10 characters. The score is
an integer between 0 and 100.
Input:
235
230
Output:
HHTTT12345
HHTTT
247. Write a Java program which accepts three integers and check whether sum of the
first two given integers is greater than third one. Three integers are in the
interval [-231, 231 ].
Input:
Input three integers (a,b,c):
5 8 9
Check whether (a + b) is greater than c?
true
Test Data
Input an integer between 0 and 1000: 565
Expected Output :
The sum of all digits in 565 is 16
4. Write a Java program to convert minutes into a number of years and days.
Test Data
Input the number of minutes: 3456789
Expected Output :
3456789 minutes is approximately 6 years and 210 days
Test Data
Input the time zone offset to GMT: 256
Expected Output:
Current time is 23:40:24
Test Data
Input weight in pounds: 452
Input height in inches: 72
Expected Output:
Body Mass Index is 61.30159143458721
7. Write a Java program to takes the user for a distance (in meters) and the time
was taken (as three numbers: hours, minutes, seconds), and display the speed, in
meters per second, kilometers per hour and miles per hour (hint: 1 mile = 1609
meters).
Test Data
Input distance in meters: 2500
Input hour: 5
Input minutes: 56
Input seconds: 23
Expected Output :
Your speed in meters/second is 0.11691531
Your speed in km/h is 0.42089513
Your speed in miles/h is 0.26158804
8. Write a Java program that reads a number and display the square, cube, and
fourth power.
Expected Output:
Square: .2f
Cube: .2f
Fourth power: 50625.00
9. Write a Java program that accepts two integers from the user and then prints the
sum, the difference, the product, the average, the distance (the difference between
integer), the maximum (the larger of the two integers), the minimum (smaller of the
two integers).
Test Data
Input 1st integer: 25
Input 2nd integer: 5
Expected Output :
Sum of two integers: 30
Difference of two integers: 20
Product of two integers: 125
Average of two integers: 15.00
Distance of two integers: 20
Max integer: 25
Min integer: 5
10. Write a Java program to break an integer into a sequence of individual digits.
Test Data
Input six non-negative digits: 123456
Expected Output :
1 2 3 4 5 6
11. Write a Java program to test whether a given double/float value is a finite
floating-point value or not.
12. Write a Java program to compare two given signed and unsigned numbers.
13. Write a Java program to compute the floor division and the floor modulus of the
given dividend and divisor.
14. Write a Java program to extract the primitive type value from a given
BigInteger value.
A primitive type is predefined by the language and is named by a reserved keyword.
Primitive values do not share state with other primitive values. The eight
primitive data types supported by the Java programming language are byte, short,
int, long, float, double, Boolean and char.
BigInteger() translates the sign-magnitude representation of a BigInteger into a
BigInteger. The sign is represented as an integer signum value: -1 for negative, 0
for zero, or 1 for positive. The magnitude is a byte array in big-endian byte-
order: the most significant byte is in the zeroth element. A zero-length magnitude
array is permissible, and will result in a BigInteger value of 0, whether signum is
-1, 0 or 1.
15. Write a Java program to get the next floating-point adjacent in the direction
of positive and negative infinity from a given float/double number.
1. Write a Java program to get a number from the user and print whether it is
positive or negative.
Test Data
Input number: 35
Expected Output :
Number is positive
2. 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
3. Take three numbers from the user and print the greatest number.
Test Data
Input the 1st number: 25
Input the 2nd number: 78
Input the 3rd number: 87
Expected Output :
The greatest: 87
4. Write a Java program that reads a floating-point number and prints "zero" if the
number is zero. Otherwise, print "positive" or "negative". Add "small" if the
absolute value of the number is less than 1, or "large" if it exceeds 1,000,000.
Test Data
Input a number: 25
Expected Output :
Input value: 25
Positive number
5. Write a Java program that keeps a number from the user and generates an integer
between 1 and 7 and displays the name of the weekday.
Test Data
Input number: 3
Expected Output :
Wednesday
6. Write a Java program that reads in two floating-point numbers and tests whether
they are the same up to three decimal places.
Test Data
Input floating-point number: 1256
Input floating-point another number: 3254
Expected Output :
They are different
Test Data
Input a month number: 2
Input a year: 2016
Expected Output :
February 2016 has 29 days
8. Write a Java program that takes the user to provide a single character from the
alphabet. Print Vowel or Consonant, depending on the user input. If the user input
is not a letter (between a and z or A and Z), or is a string of length > 1, print
an error message.
Test Data
Input an alphabet: p
Expected Output :
Input letter is Consonant
9. Write a Java program that takes a year from user and print whether that year is
a leap year or not.
Test Data
Input the year: 2016
Expected Output :
2016 is a leap year
Expected Output :
1
2
3
4
5
6
7
8
9
10
11. Write a program in Java to display n terms of natural numbers and their sum.
Test Data
Input the number: 2
Expected Output :
Input number:
2
The first n natural numbers are :
2
1
2
The Sum of Natural Number upto n terms :
23
.
12. Write a program in Java to input 5 numbers from keyboard and find their sum and
average.
Test Data
Input the 5 numbers : 1 2 3 4 5
Expected Output :
13. Write a program in Java to display the cube of the number upto given an
integer.
Test Data
Input number of terms : 4
Expected Output :
14. Write a program in Java to display the multiplication table of a given integer.
Test Data
Input the number (Table to be calculated) : Input number of terms : 5
Expected Output :
5 X 0 = 0
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
15. Write a program in Java to display the n terms of odd natural number and their
sum.
Test Data
Input number of terms is: 5
Expected Output :
16. Write a program in Java to display the pattern like right angle triangle with a
number.
Test Data
Input number of rows : 10
Expected Output :
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
17. Write a program in Java to make such a pattern like right angle triangle with a
number which will repeat a number in a row.The pattern is as follows :
1
22
333
4444
18. Write a program in Java to make such a pattern like right angle triangle with
number increased by 1.The pattern like :
1
2 3
4 5 6
7 8 9 10
19. Write a program in Java to make such a pattern like a pyramid with a number
which will repeat the number in the same row.
1
2 2
3 3 3
4 4 4 4
Test Data
Input number of rows : 5
Expected Output :
1
01
101
0101
10101
Test Data
Input number of rows (half of the diamond) : 7
Expected Output :
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
Test Data
Input number of rows: 5
Expected Output :
Test Data
Input the number: 6
Expected Output :
******
*****
****
***
**
*
Test Data
Input the number: 6
Expected Output :
@
@@
@@@
@@@@
@@@@@
@@@@@@
Test Data
Input the number: 7
Expected Output :
1
212
32123
4321234
543212345
65432123456
7654321234567
65432123456
543212345
4321234
32123
212
1
26. Write a Java program to display the following character rhombus structure.
Test Data
Input the number: 7
Expected Output :
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFEDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
27. Write a Java program that reads an integer and check whether it is negative,
zero, or positive.
Test Data
Input a number: 7
Expected Output :
Number is positive
28. Write a Java program that reads a floating-point number. If the number is zero
it prints "zero", otherwise, print "positive" or "negative". Add "small" if the
absolute value of the number is less than 1, or "large" if it exceeds 1,000,000.
Test Data
Input a number: -2534
Expected Output :
Negative
29. Write a Java program that reads an positive integer and count the number of
digits the number (less than ten billion) has.
Test Data
Input an integer number less than ten billion: 125463
Expected Output :
30. Write a Java program that accepts three numbers and prints "All numbers are
equal" if all three numbers are equal, "All numbers are different" if all three
numbers are different and "Neither all are equal or different" otherwise.
Test Data
Input first number: 2564
Input second number: 3526
Input third number: 2456
Expected Output :
Test Data
Input first number: 1524
Input second number: 2345
Input third number: 3321
Expected Output :
Increasing order
32. Write a Java program that accepts two floatingpoint numbers and checks whether
they are the same up to two decimal places.
Test Data
Input first floatingpoint number: 1235
Input second floatingpoint number: 2534
Expected Output :
Expected Output :
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
10. Write a Java program to find the maximum and minimum value of an array.
12. Write a Java program to find the duplicate values of an array of integer
values.
13. Write a Java program to find the duplicate values of an array of string values.
14. Write a Java program to find the common elements between two arrays (string
values).
15. Write a Java program to find the common elements between two arrays of
integers.
17. Write a Java program to find the second largest element in an array.
18. Write a Java program to find the second smallest element in an array.
19. Write a Java program to add two matrices of the same size.
20. Write a Java program to convert an array to ArrayList.
22. Write a Java program to find all pairs of elements in an array whose sum is
equal to a specified number.
25. Write a Java program to find common elements from three sorted (in non-
decreasing order) arrays.
26. Write a Java program to move all 0's to the end of an array. Maintain the
relative order of the other (non-zero) array elements.
27. Write a Java program to find the number of even and odd integers in a given
array of integers.
28. Write a Java program to get the difference between the largest and smallest
values in an array of integers. The length of the array must be 1 and above.
29. Write a Java program to compute the average value of an array of integers
except the largest and smallest values.
30. Write a Java program to check if an array of integers without 0 and -1.
31. Write a Java program to check if the sum of all the 10's in the array is
exactly 30. Return false if the condition does not satisfy, otherwise true.
32. Write a Java program to check if an array of integers contains two specified
elements 65 and 77.
33. Write a Java program to remove the duplicate elements of a given array and
return the new length of the array.
Sample array: [20, 20, 30, 40, 50, 50, 50]
After removing the duplicate elements the program should return 4 as the new length
of the array.
34. Write a Java program to find the length of the longest consecutive elements
sequence from a given unsorted array of integers.
Sample array: [49, 1, 3, 200, 2, 4, 70, 5]
The longest consecutive elements sequence is [1, 2, 3, 4, 5], therefore the program
will return its length 5.
35. Write a Java program to find the sum of the two elements of a given array which
is equal to a given integer.
Sample array: [1,2,4,5,6]
Target value: 6.
36. Write a Java program to find all the unique triplets such that sum of all the
three elements [x, y, z (x ≤ y ≤ z)] equal to a specified number.
Sample array: [1, -2, 0, 5, -1, -4]
Target value: 2.
37. Write a Java program to create an array of its anti-diagonals from a given
square matrix.
Example:
Input :
1 2
3 4
Output:
[
[1],
[2, 3],
[4]
]
38. Write a Java program to get the majority element from an given array of
integers containing duplicates.
Majority element: A majority element is an element that appears more than n/2 times
where n is the size of the array.
39. Write a Java program to print all the LEADERS in the array.
Note: An element is leader if it is greater than all the elements to its right
side.
40. Write a Java program to find the two elements from a given array of positive
and negative numbers such that their sum is closest to zero.
41. Write a Java program to find smallest and second smallest elements of a given
array.
42. Write a Java program to segregate all 0s on left side and all 1s on right side
of a given array of 0s and 1s.
43. Write a Java program to find all combination of four elements of an given array
whose sum is equal to a given value.
44. Write a Java program to count the number of possible triangles from an given
unsorted array of positive integers.
45. Write a Java program to cyclically rotate a given array clockwise by one.
46. Write a Java program to check whether there is a pair with a specified sum of a
given sorted and rotated array.
47. Write a Java program to find the rotation count in a given rotated sorted array
of integers.
48. Write a Java program to arrange the elements of an given array of integers
where all negative integers appear before all the positive integers.
49. Write a Java program to arrange the elements of an given array of integers
where all positive integers appear before all the negative integers.
50. Write a Java program to sort an array of positive integers of an given array,
in the sorted array the value of the first element should be maximum, second value
should be minimum value, third should be second maximum, fourth second be second
minimum and so on.
51. Write a Java program to separate 0s on left side and 1s on right side of an
array of 0s and 1s in random order.
52. Write a Java program to separate even and odd numbers of an given array of
integers. Put all even numbers first, and then odd numbers.
53. Write a Java program to replace every element with the next greatest element
(from right side) in an given array of integers.
1. Write a Java program to get the character at the given index within the String.
Sample Output:
2. Write a Java program to get the character (Unicode code point) at the given
index within the String.
Sample Output:
3. Write a Java program to get the character (Unicode code point) before the
specified index within the String.
Sample Output:
4. Write a java program to count a number of Unicode code points in the specified
text range of a String.
Sample Output:
Sample Output:
String 1: This is Exercise 1
String 2: This is Exercise 2
"This is Exercise 1" is less than "This is Exercise 2"
Sample Output:
7. Write a Java program to concatenate a given string to the end of another string.
Sample Output:
8. Write a Java program to test if a given string contains the specified sequence
of char values.
Sample Output:
Sample Output:
10. Write a Java program to compare a given string to the specified string buffer.
Sample Output:
11. Write a Java program to create a new String object with the contents of a
character array.
Sample Output:
Sample Output:
13. Write a Java program to check whether two String objects contain the same data.
Sample Output:
14. Write a Java program to compare a given string to another string, ignoring case
considerations.
Sample Output:
15. Write a java program to print current date and time in the specified format.
Sample Output:
16. Write a Java program to get the contents of a given string as a byte array.
Sample Output:
17. Write a Java program to get the contents of a given string as a character
array.
Sample Output:
Sample Output:
Sample Output:
a b c d e f g h i j
=========================
36 10 7 40 2 16 42 1 6 20
k l m n o p q r s t
===========================
8 35 22 14 12 23 4 11 24 31
u v w x y z
================
5 27 13 18 38 37
Sample string of all alphabet: "The quick brown fox jumps over the lazy dog."
20. Write a Java program to get the canonical representation of the string object.
Sample Output:
21. Write a Java program to get the last index of a string within a string.
Sample Output:
a b c d e f g h i j
===========================
36 10 7 40 33 16 42 32 6 20
k l m n o p q r s t
===========================
8 35 22 14 41 23 4 29 24 31
u v w x y z
=================
21 27 13 18 38 37
Sample string of all alphabet: "The quick brown fox jumps over the lazy dog."
Sample Output:
23. Write a Java program to find whether a region in the current string matches a
region in another string.
Sample Output:
str1[0 - 7] == str2[28 - 35]? true
str1[9 - 15] == str2[9 - 15]? false
24. Write a Java program to replace all the 'd' characters with 'f' characters.
Sample Output:
Original string: The quick brown fox jumps over the lazy dog.
New String: The quick brown fox jumps over the lazy fog.
25. Write a Java program to replace each substring of a given string that matches
the given regular expression with the given replacement.
Sample string : "The quick brown fox jumps over the lazy dog."
Sample Output:
Original string: The quick brown fox jumps over the lazy dog.
New String: The quick brown cat jumps over the lazy dog.
26. Write a Java program to check whether a given string starts with the contents
of another string.
Sample Output:
27. Write a Java program to get a substring of a given string between two specified
positions.
Sample Output:
old = The quick brown fox jumps over the lazy dog.
new = brown fox jumps
28. Write a Java program to create a character array containing the contents of a
string.
Sample Output:
Java Exercises.
29. Write a Java program to convert all the characters in a string to lowercase.
Sample Output:
Sample Output:
31. Write a Java program to trim any leading or trailing whitespace from a given
string.
Sample Output:
32. Write a Java program to find longest Palindromic Substring within a string.
Sample Output:
Sample Output:
34. Write a Java program to find the second most frequent character in a given
string.
Sample Output:
35. Write a Java program to print all permutations of a given string with
repetition.
Sample Output:
36. Write a Java program to check whether two strings are interliving of a given
string. Assuming that the unique characters in both strings.
Sample Output:
37. Write a Java program to find Length of the longest substring without repeating
characters.
Sample Output:
38. Write a Java program to print after removing duplicates from a given string.
Sample Output:
39. Write a Java program to find first non repeating character in a string.
Sample Output:
Sample Output:
abcde
fghij
klmno
pqrst
uvwxy
41. Write a Java program to remove duplicate characters from a given string
presents in another given string.
Sample Output:
42. Write a Java program to print list items containing all characters of a given
word.
Sample Output:
The strings containing all the letters of the given word are:
rabbit
bribe
43. Write a Java program to find the maximum occurring character in a string.
Sample Output:
Sample Output:
Sample Output:
46. Write a Java program to reverse every word in a string using methods.
Sample Output:
47. Write a Java program to rearrange a string so that all same characters become d
distance away.
Sample Output:
48. Write a Java program to remove "b" and "ac" from a given string.
Sample Output:
49. Write a Java program to find first non-repeating character from a stream of
characters.
Sample Output:
String: godisgood
Reading: g
The first non-repeating character so far is: g
Reading: o
The first non-repeating character so far is: g
Reading: d
The first non-repeating character so far is: g
Reading: i
The first non-repeating character so far is: g
Reading: s
The first non-repeating character so far is: g
Reading: g
The first non-repeating character so far is: o
Reading: o
The first non-repeating character so far is: d
Reading: o
The first non-repeating character so far is: d
Reading: d
The first non-repeating character so far is: i
Sample Output:
Sample Output:
52. Write a Java program to check if two given strings are rotations of each other.
Sample Output:
53. Write a Java program to match two strings where one string contains wildcard
characters.
Sample Output:
54. Write a Java program to find the smallest window in a string containing all
characters of another string.
Sample Output:
55. Write a Java program to remove all adjacent duplicates recursively from a given
string.
Sample Output:
56. Write a Java program to append two given strings such that, if the
concatenation creates a double characters then omit one of the characters.
Sample Output:
57. Write a Java program to return a new string where the last two characters of a
given string, if present, are swapped.
Sample Output:
58. Write a Java program to read a string and return true if it ends in "ng".
Sample Output:
59. Write a Java program to read a string,if the string begins with "red"
or "black" return that color string, otherwise return the empty string.
Sample Output:
60. Write a Java program to read two strings append them together and return the
result. If the strings are different lengths, omit chars from the beginning of
longer string and make them equal length.
Sample Output:
61. Write a Java program to read a string and an int n, return a string made of the
first and last n characters from the string.
Sample Output:
62. Write a Java program to read a string and return true if "good" appears
starting at index 0 or 1 in the given string.
Sample Output:
Sample Output:
64. Write a Java program to read a string and if a substring of length two appears
at both its beginning and end, return a string without the substring at the
beginning otherwise, return the original string unchanged.
Sample Output:
65. Write a Java program to read a string and if the first or last characters are
't', return the string without those 't' otherwise return the string unchanged.
Sample Output:
66. Write a Java program to read a string and return the string without the first
two characters.Except keep the first char if it is 'g' and keep the second char if
it is 'h'.
Sample Output:
67. Write a Java program to read a string and if one or both of the first tow
characters is 'x', return without those 'x',otherwise return the string unchanged.
Sample Output:
Sample Output:
69. Write a Java program to return the substring that is between the first and last
appearance of the substring 'toast' in the given string,or return the empty string
if substirng 'toast' does not exists.
Sample Output:
70. Write a Java program to check whether a string is pq-balanced or not.A String
is pq-balanced if for all the p's in the string atleast one 'q' must exists right
of the p's.But 'q' before the 'p' makes the pq-balanced false.
Sample Output:
71. Write a Java program to return true when either of the two given strings appear
at the end of the other string ignoring case sensitivity.
Sample Output:
72. Write a Java program to return true if a given string contain the string 'pop',
but the middle 'o' also may other character.
Sample Output:
73. Write a Java program to return true if the given string contains an appearance
of 'abc' but not directly a period(.) and followed by.
Sample Output:
74. Write a Java program to return whether a prefix string made of the first N
specific characters of the string appear somewhere else in the string.
Sample Output:
75. Write a Java program to check whether a string 'abc' in the middle of a given
string. Here middle means the number of character to the left and right of the
substring 'abc' must differ by at most one.
Sample Output:
76. Write a Java program to count how many times the substring 'life' present at
anywhere in a given string. Counting can also happen for the substring 'li?e', any
character instead of 'f'.
Sample Output:
77. Write a Java program to add a string with specific number of times seperated by
a substring.
Sample Output:
The given strings are: try and best
Number to times to be repeat: 3
The new string is: trybesttrybesttry
78. Write a Java program to repeat a specific number of characters for specific
number of times from the last of a string.
Sample Output:
79. Write a Java program to return the given string after removing the 2nd
character from the substring of length three, starting with 'z' and ending with
'g'.
Sample Output:
80. Write a Java program to check whether the character immediately before and
after of # is same in a given string.
Sample Output:
81. Write a Java program to check whether the string 'red' and 'blue' appear in
same number of times in a given string.
Sample Output:
82. Write a Java program to repeat every character twice in the original string.
Sample Output:
83. Write a Java program to make a new string from two given string in such a way
that, each character of two string will come respectively.
Sample Output:
84. Write a Java program to make a new string made of p number of characters from
the first of a given string and followed by p-1 number characters till the p is
greater than zero.
Sample Output:
85. Write a Java program to make a new string with each character of just before
and after of t-string whichever it appears in m-string. Assume that m-string and
non-empty t-string has given.
Sample Output:
86. Write a Java program to return the number of triples in the given string. A
triple is a character appearing three times in a row in a string.
Sample Output:
87. Write a Java program to check whether a z is happy or not. A 'z' is happy when
there is another 'z' immediately to its left or right.Return true if all the z's in
the given string are happy.
Sample Output:
88. Write a Java program to return a string where every appearance of the lowercase
word 'is' has been replaced with 'is not'.
Sample Output:
89. Write a Java program to return the sum of the numbers (may form more than one
digits), appearing in the string.
Sample Output:
90. Write a Java program to return true if the number of appearances of 'the' and
'is' anywhere in the string is equal.
Sample Output:
91. Write a Java program to count the number of words ending in 'm' or 'n' (not
case sensitive).
Sample Output:
92. Write a Java program to return a substring after removing the all instances of
remove string as given from the given main string.
Sample Output:
93. Write a Java program to find the longest substring appears at both ends of a
given string.
Sample Output:
94. Write a Java program to find the longest mirror image string at the both ends
of a given string.
Sample Output:
95. Write a Java program to return the sum of the digits present in the given
string. If there is no digits the sum return is 0.
Sample Output:
The given string is: ab5c2d4ef12s
The sum of the digits in the string is: 14
96. Write a Java program to return the string after removing all 'z' (except the
very first and last) from a given string.
Sample Output:
97. Write a Java program to return a string with the characters of the index
position 0,1,2, 5,6,7, ... from a given string.
Sample Output:
98. Write a Java program to check whether the first instance of 'z' is immediately
followed by another 'z' in a given string.
Sample Output:
99. Write a Java program to return a new string using every characters of even
positions from a given string.
Sample Output:
100. Write a Java program to check if a given string contains another string.
Return true or false.
Sample Output:
Original string:
Java is the foundation for virtually every type of networked application and is the
global standard for developing and delivering embedded and mobile applications,
games, Web-based content, and enterprise software. With more than 9 million
developers worldwide, Java enables you to efficiently develop, deploy and use
exciting applications and services.
Sample Output:
First string:
131231231231231231231231231212312312
true
Second string:
13123123123Z1231231231231231212312312
false
102. Write a Java program to convert a given String to int, long, float and double.
Sample Output:
103. Write a Java program to remove a specified character from a given string.
Sample Output:
Original string:
abcdefabcdeabcdaaa
Second string:
bcdefbcdebcd
104. Write a Java program to sort in ascending and descending order by length of
the given array of strings.
Sample Output:
Original unsorted colors: [Green, White, Black, Pink, Orange, Blue, Champagne,
Indigo, Ivory]
Sorted color (descending order): [Champagne, Orange, Indigo, Green, White, Black,
Ivory, Pink, Blue]
Sorted color (ascending order): [Pink, Blue, Green, White, Black, Ivory, Orange,
Indigo, Champagne]
105. Write a Java program to count the occurrences of a given string in another
given string.
Sample Output:
106. Write a Java program to concatenate a given string with itself of a given
number of times.
Sample Output:
1. Write a Java program to create a Date object using the Calendar class.
2. Write a Java program to get and display information (year, month, day, hour,
minute) of a default calendar.
3. Write a Java program to get the maximum value of the year, month, week, date
from the current date of a default calendar.
4. Write a Java program to get the minimum value of year, month, week, date from
the current date of a default calendar.
7. Write a Java program to get the last day of the current month.
9. Write a Java program to calculate the first and last day of each week.
10. Write a Java program to get the name of the first and last day of a month.
13. Write a Java program to get a day of the week of a specific date.
15. Write a Java program to add some hours to the current time.
17. Write a Java program to get a date before and after 1 year compares to the
current date.
19. Write a Java program to get year and months between two dates.
21. Write a Java program to get the current time in all the available time zones.
22. Write a Java program to get the dates 10 days before and after today.
23. Write a Java program to get the months remaining in the year.
24. Write a Java program to display the dates in the following formats.
Sample format :
Sample format :
Sample Output :
Current Year: 2001
Is current year leap year? false
Length of the year: 365 days
Sample format :
Sample Output :
Integer value of the current month: 2
Length of the month: 28
Maximum length of the month: 29
First month of the Quarter: JANUARY
28. Write a Java program to display the date time information before some hours and
minutes from current date time.
30. Write a Java program to compute the difference between two dates (year, months,
days).
31. Write a Java program to compute the difference between two dates (Hours,
minutes, milli, seconds and nano).
35. Write a Java program to extract date, time from the date string.
38. Write a Java program to calculate the difference between two dates in days.
39. Write a java program to convert String to date and time and vice a versa.
40. Write a Java program to display current date without time and current time
without date.
41. Write a Java program to display combine local date and time in a single object.
42. Write a Java program to define a period of time using date-based values
(Period) and a duration of time using time-based values (Duration).
43. Write a Java program to display all the available time zones with UTC and GMT.
1. Write a Java method to find the smallest number among three numbers.
Test Data:
Input the first number: 25
Input the Second number: 37
Input the third number: 29
Expected Output:
The sum is 7
1 5 12 22 35 51 70 92 117 145
176 210 247 287 330 376 425 477 532 590
651 715 782 852 925 1001 1080 1162 1247 1335
1426 1520 1617 1717 1820 1926 2035 2147 2262 2380
2501 2625 2752 2882 3015 3151 3290 3432 3577 3725
8. Write a Java method to compute the future investment value at a given interest
rate for a specified number of years.
Sample data (Monthly compounded) and Output:
Input the investment amount: 1000
Input the rate of interest: 10
Input number of years: 5
Expected Output:
Years FutureValue
1 1104.71
2 1220.39
3 1348.18
4 1489.35
5 1645.31
Expected Output:
( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ;
< = > ? @ A B C D E F G H I J K L M N O
P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c
d e f g h i j k l m n o p q r s t u v w
x y z
10. Write a Java method to check whether a year (integer) entered by the user is a
leap year or not.
Expected Output:
Expected Output:
Expected Output:
Input a number: 10
1 0 0 1 1 0 0 0 1 1
0 0 1 0 1 0 1 0 0 0
0 1 0 1 0 0 0 0 0 1
1 1 1 0 0 0 0 1 1 1
1 1 0 1 1 1 0 1 0 0
1 0 0 0 1 1 0 0 0 0
0 0 1 0 0 0 0 1 1 1
1 1 0 1 0 1 0 0 1 0
0 0 1 0 0 0 0 1 1 0
1 1 1 0 0 1 1 1 1 0
Expected Output:
Input Side-1: 10
Input Side-2: 15
Input Side-3: 20
The area of the triangle is 72.6184377413890
Expected Output:
15. Write a Java method to display the current date and time.
Expected Output:
16. Write a Java method to find all twin prime numbers less than 100.
Expected Output:
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)
1. Write a Java program to check whether a given number is an ugly number.
In number system, ugly numbers are positive numbers whose only prime factors are 2,
3 or 5. First 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12. By convention, 1
is included.
Test Date:Input an integer number: 235
Expected Output :
It is not an ugly number.
4. Write a Java program to generate and show all Kaprekar numbers less than 1000.
Expected Output :
1 1 0 + 1
9 81 8 + 1
45 2025 20 + 25
55 3025 30 + 25
99 9801 98 + 01
297 88209 88 + 209
703 494209 494 + 209
999 998001 998 + 001
8 Kaprekar numbers.
5. Write a Java program to find the number of seed Lychrel number candidates and
related numbers for n in the range 1..10000 inclusive. (With that iteration limit
of 500).
A Lychrel number is a natural number that cannot form a palindrome through the
iterative process of repeatedly reversing its digits and adding the resulting
numbers. This process is sometimes called the 196-algorithm, after the most famous
number associated with the process.
The first few Lychrel numbers are 196, 295, 394, 493, 592, 689, 691, 788, 790, 879,
887, ... .
Expected Output :
5 Lychrel seeds: [196, 879, 1997, 7059, 9999]
244 Lychrel related
5 Lychrel palindromes: [196, 879, 1997, 7059, 9999]
6. Write a Java program to generate and show the first 15 narcissistic decimal
numbers.
Expected Output :
8.Write a Java program to print out the first 10 Catalan numbers by extracting them
from Pascal's triangle.
In combinatorial mathematics, the Catalan numbers form a sequence of natural
numbers that occur in various counting problems, often involving recursively-
defined objects. They are named after the Belgian mathematician Eugène Charles
Catalan.
The first Catalan numbers for n = 0, 1, 2, 3, … are 1, 1, 2, 5, 14, 42, 132, 429,
1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790,
477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650,
1289904147324, 4861946401452,
1
2
5
14
42
132
429
1430
4862
16796
9. Write a Java program to find and print the first 10 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
82 + 22=68
62 + 82=100
12 + 02 + 02=1
Expected Output
10. Write a Java program to check whether a given number is a happy number or
unhappy number.
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.
An unhappy number is a number that is not happy.
The first few unhappy numbers are 2, 3, 4, 5, 6, 8, 9, 11, 12, 14, 15, 16, 17, 18,
20.
Expected Output
Input a number: 5
Unhappy Number
11. 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 175 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.
12. Write a Java program to check whether a number is a Harshad Number or not.
In recreational mathematics, a harshad number in a given number base, is an integer
that is divisible by the sum of its digits when written in that base.
Example: Number 200 is a Harshad Number because the sum of digits 2 and 0 and 0 is
2(2+0+0) and 200 is divisible by 2. Number 171 is a Harshad Number because the sum
of digits 1 and 7 and 1 is 9(1+7+1) and 171 is divisible by 9.
Expected Output
Expected Output
Input a number : 110
Pronic Number.
14. Write a Java program check whether a number is an Automorphic number or not.
In mathematics, an automorphic number is a number whose square "ends" in the same
digits as the number itself. For example, 52 = 25, 62 = 36, 762 = 5776, and 8906252
= 793212890625, so 5, 6, 76 and 890625 are all automorphic numbers.
Expected Output
Input a number : 76
Automorphic Number.
15. Write a Java program to check whether a number is a Duck Number or not.
Note: A Duck number is a number which has zeroes present in it, but there should be
no zero present in the beginning of the number. For example 3210, 7056, 8430709 are
all duck numbers whereas 08237, 04309 are not.
Expected Output
17. Write a Java program to check if a given number is circular prime or not.
Circular Prime : A circular prime is a prime number with the property that the
number generated at each intermediate step when cyclically permuting its (base 10)
digits will be prime.
For example, 1193 is a circular prime, since 1931, 9311 and 3119 all are also
prime. A circular prime with at least two digits can only consist of combinations
of the digits 1, 3, 7 or 9, because having 0, 2, 4, 6 or 8 as the last digit makes
the number divisible by 2, and having 0 or 5 as the last digit makes it divisible
by 5.
Input Data:
Input a number: 35
Expected Output
Number is a cube.
It is a cyclic number.
3.0
5.0
17.0
257.0
65537.0
4.294967297E9
1.8446744073709552E19
3.4028236692093846E38
1.157920892373162E77
1.3407807929942597E154
Infinity
21. Write java program to find any number between 1 and n that can be expressed as
the sum of two cubes in two (or more) different ways.
//http://introcs.cs.princeton.edu/java/13flow/Ramanujan.java.html
Here are some examples of Ramanujan numbers :
1729 = 1^3 + 12^3 = 9^3 + 10^3
* 10000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
* 100000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
13832 = 2^3 + 24^3 = 18^3 + 20^3
39312 = 2^3 + 34^3 = 15^3 + 33^3
46683 = 3^3 + 36^3 = 27^3 + 30^3
32832 = 4^3 + 32^3 = 18^3 + 30^3
40033 = 9^3 + 34^3 = 16^3 + 33^3
20683 = 10^3 + 27^3 = 19^3 + 24^3
65728 = 12^3 + 40^3 = 31^3 + 33^3
64232 = 17^3 + 39^3 = 26^3 + 36^3
Expected Output
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
13832 = 2^3 + 24^3 = 18^3 + 20^3
39312 = 2^3 + 34^3 = 15^3 + 33^3
46683 = 3^3 + 36^3 = 27^3 + 30^3
32832 = 4^3 + 32^3 = 18^3 + 30^3
40033 = 9^3 + 34^3 = 16^3 + 33^3
20683 = 10^3 + 27^3 = 19^3 + 24^3
65728 = 12^3 + 40^3 = 31^3 + 33^3
64232 = 17^3 + 39^3 = 26^3 + 36^3
Expected Output
Input a number: 127
127 is a Mersenne number.
23. Write a Java program to find all the narcissistic numbers between 1 and 1000.
In number theory, a narcissistic number is a number that is the sum of its own
digits each raised to the power of the number of digits.
For example:
153 = 13 + 53 + 33
Expected Output
1
2
3
4
5
6
7
8
9
153
370
371
407
Expected Output
Input a number: 5
It is a Palindrome number.
25. Write a Java program to print the first 15 numbers of the Pell series.
In mathematics, the Pell numbers are an infinite sequence of integers. The sequence
of Pell numbers starts with 0 and 1, and then each Pell number is the sum of twice
the previous Pell number and the Pell number before that.:
thus, 70 is the companion to 29, and 70 = 2 × 29 + 12 = 58 + 12.
The first few terms of the sequence are :
0, 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741, 13860,…
Expected Output
First 20 Pell numbers:
1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 113
6689 2744210 6625109 15994428
26. Write a Program in Java to check whether a number is a Keith Number or not.
In recreational mathematics, a Keith number or repfigit number (short for
repetitive Fibonacci-like digit) is a number in the following integer sequence:
14, 19, 28, 47, 61, 75, 197, 742, 1104, 1537, 2208, 2580, 3684, 4788, 7385, 7647,
7909, 31331, 34285, 34348, 55604, 62662, 86935, 93993, 120284, 129106, 147640,
156146, 174680, 183186, 298320, 355419, 694280, 925993,
Expected Output
Input a number: 75
Keith Number
27. Write a Java program to create the first twenty Hamming numbers.
In computer science, regular numbers are often called Hamming numbers, Hamming
Numbers are numbers whose only prime factors are 2, 3 and 5.
The first few hamming numbers are :
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32
Expected Output
First Twenty Hamming numbers: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27
30 32 36
1. Write a Java program to get a list of all file/directory names from the given.
4. Write a Java program to check if a file or directory has read and write
permission.
10. Write a Java program to read contents from a file into byte array.
11. Write a Java program to read a file content line by line.
13. Write a java program to read a file line by line and store it into a variable.
14. Write a Java program to store text file content line by line into an array.
15. Write a Java program to write and read a plain text file.
18. Write a Java program to find the longest word in a text file.
1. Write a Java program to create a new array list, add some colors (string) and
print out the collection.
3. Write a Java program to insert an element into the array list at the first
position.
4. Write a Java program to retrieve an element (at a specified index) from a given
array list.
6. Write a Java program to remove the third element from a array list.
16. Write a Java program to clone an array list to another array list.
19. Write a Java program to trim the capacity of an array list the current list
size.
21. Write a Java program to replace the second element of a ArrayList with the
specified element.
22. Write a Java program to print all the elements of a ArrayList using the
position of the elements.
3. Write a Java program to iterate through all elements in a linked list starting
at the specified position.
6. Write a Java program to insert elements into the linked list at the first and
last position.
7. Write a Java program to insert the specified element at the front of a linked
list.
8. Write a Java program to insert the specified element at the end of a linked
list.
9. Write a Java program to insert some elements at the specified position into a
linked list.
10. Write a Java program to get the first and last occurrence of the specified
elements in a linked list.
11. Write a Java program to display the elements and their positions in a linked
list.
12. Write a Java program to remove a specified element from a linked list.
13. Write a Java program to remove first and last element from a linked list.
14. Write a Java program to remove all the elements from a linked list.
18. Write a Java program to clone an linked list to another linked list.
19. Write a Java program to remove and return the first element of a linked list.
20. Write a Java program to retrieve but does not remove, the first element of a
linked list.
21. Write a Java program to retrieve but does not remove, the last element of a
linked list.
22. Write a Java program to check if a particular element exists in a linked list.
11. Write a Java program to compare two sets and retain elements which are same on
both sets.
12. Write a Java program to remove all of the elements from a hash set.
3. Write a Java program to add all the elements of a specified tree set to another
tree set.
4. Write a Java program to create a reverse order view of the elements contained in
a given tree set.
5. Write a Java program to get the first and last elements in a tree set.
6. Write a Java program to clone a tree set list to another tree set.
9. Write a Java program to find the numbers less than 7 in a tree set.
10. Write a Java program to get the element in a tree set which is greater than or
equal to the given element.
11. Write a Java program to get the element in a tree set which is less than or
equal to the given element.
12. Write a Java program to get the element in a tree set which is strictly greater
than or equal to the given element.
13. Write a Java program to get an element in a tree set which is strictly less
than the given element.
14. Write a Java program to retrieve and remove the first element of a tree set.
15. Write a Java program to retrieve and remove the last element of a tree set.
16. Write a Java program to remove a given element from a tree set.
5. Write a Java program to remove all the elements from a priority queue.
8. Write a Java program to retrieve the first element of the priority queue.
10. Write a Java program to convert a priority queue to an array containing all of
the elements of the queue.
2. Write a Java program to count the number of key-value (size) mappings in a map.
3. Write a Java program to copy all of the mappings from the specified map to
another map.
5. Write a Java program to check whether a map contains key-value mappings (empty)
or not.
7. Write a Java program to test if a map contains a mapping for the specified key.
8. Write a Java program to test if a map contains a mapping for the specified
value.
9. Write a Java program to create a set view of the mappings contained in a map.
10. Write a Java program to get the value of a specified key in a map.
11. Write a Java program to get a set view of the keys contained in this map.
12. Write a Java program to get a collection view of the values contained in this
map.
2. Write a Java program to copy a Tree Map content to another Tree Map.
5. Write a Java program to get all keys from the given a Tree Map.
6. Write a Java program to delete all elements from a given Tree Map.
8. Write a Java program to get a key-value mapping associated with the greatest key
and the least key in a map.
9. Write a Java program to get the first (lowest) key and the last (highest) key
currently in a map.
10. Write a Java program to get a reverse order view of the keys contained in a
given map.
11. Write a Java program to get a key-value mapping associated with the greatest
key less than or equal to the given key.
12. Write a Java program to get the greatest key less than or equal to the given
key.
13. Write a Java program to get the portion of a map whose keys are strictly less
than a given key.
14. Write a Java program to get the portion of this map whose keys are less than
(or equal to, if inclusive is true) a given key.
15. Write a Java program to get the least key strictly greater than the given key.
Return null if there is no such key.
16. Write a Java program to get a key-value mapping associated with the greatest
key strictly less than the given key. Return null if there is no such key.
17. Write a Java program to get the greatest key strictly less than the given key.
Return null if there is no such key.
18. Write a Java program to get NavigableSet view of the keys contained in a map.
19. Write a Java program to remove and get a key-value mapping associated with the
least key in a map.
20. Write a Java program to remove and get a key-value mapping associated with the
greatest key in this map.
21. Write a Java program to get the portion of a map whose keys range from a given
key (inclusive), to another key (exclusive).
22. Write a Java program to get the portion of a map whose keys range from a given
key to another key.
23. Write a Java program to get a portion of a map whose keys are greater than or
equal to a given key.
24. Write a Java program to get a portion of a map whose keys are greater than to a
given key.
25. Write a Java program to get a key-value mapping associated with the least key
greater than or equal to the given key. Return null if there is no such key.
26. Write a Java program to get the least key greater than or equal to the given
key. Returns null if there is no such key.
1. Write a Java program to round up the result of integer division.
2. Write a Java program to get whole and fractional parts from a double value.
10. Write a Java program to accept a float value of number and return a rounded
float value.
11. Write a Java program to accept two integers and return true if the either one
is 15 or if their sum or difference is 15.
12. Write a Java program to count the number of prime numbers less than a given
positive number.
13. Write a Java program to find the length of the longest sequence of zeros in
binary representation of an integer.
2. Write a Java program to get whole and fractional parts from a double value.
10. Write a Java program to accept a float value of number and return a rounded
float value.
11. Write a Java program to accept two integers and return true if the either one
is 15 or if their sum or difference is 15.
12. Write a Java program to count the number of prime numbers less than a given
positive number.
13. Write a Java program to find the length of the longest sequence of zeros in
binary representation of an integer.
1. Write a Java program to sort an array of given integers using Quick sort
Algorithm.
Quick sort is a comparison sort, meaning that it can sort items of any type for
which a "less-than" relation (formally, a total order) is defined.
2. Write a Java program to sort an array of given integers using the Bubble sorting
Algorithm.
According to Wikipedia "Bubble sort, sometimes referred to as sinking sort, is a
simple sorting algorithm that repeatedly steps through the list to be sorted,
compares each pair of adjacent items and swaps them if they are in the wrong order.
The pass through the list is repeated until no swaps are needed, which indicates
that the list is sorted. The algorithm, which is a comparison sort, is named for
the way smaller elements "bubble" to the top of the list. Although the algorithm is
simple, it is too slow and impractical for most problems even when compared to
insertion sort. It can be practical if the input is usually in sort order but may
occasionally have some out-of-order elements nearly in position."
3. Write a Java program to sort an array of given integers using Radix sort
Algorithm.
According to Wikipedia "In computer science, radix sort is a non-comparative
integer sorting algorithm that sorts data with integer keys by grouping keys by the
individual digits which share the same significant position and value".
4. Write a Java program to sort an array of given integers using Merge sort
Algorithm.
According to Wikipedia "Merge sort (also commonly spelled mergesort) is an O (n log
n) comparison-based sorting algorithm. Most implementations produce a stable sort,
which means that the implementation preserves the input order of equal elements in
the sorted output."
5. Write a Java program to sort an array of given integers using Heap sort
Algorithm.
6. Write a Java program to sort an array of given integers using Selection Sort
Algorithm.
7. Write a Java program to sort an array of given integers using Insertion sort
Algorithm.
Insertion sort is a simple sorting algorithm that builds the final sorted array (or
list) one item at a time. It is much less efficient on large lists than other
algorithms such as quicksort, heapsort, or merge sort.
8. Write a Java program to sort an array of positive integers using the Bead Sort
Algorithm.
According to Wikipedia "Bead sort, also called gravity sort, is a natural sorting
algorithm, developed by Joshua J. Arulanandham, Cristian S. Calude and Michael J.
Dinneen in 2002, and published in The Bulletin of the European Association for
Theoretical Computer Science. Both digital and analog hardware implementations of
bead sort can achieve a sorting time of O(n); however, the implementation of this
algorithm tends to be significantly slower in software and can only be used to sort
lists of positive integers. Also, it would seem that even in the best case, the
algorithm requires O(n2) space".
9. Write a Java program to sort an array of positive integers using the BogoSort
Sort Algorithm.
In computer science, bogosort is a particularly ineffective sorting algorithm based
on the generate and test paradigm. The algorithm successively generates
permutations of its input until it finds one that is sorted. It is not useful for
sorting but may be used for educational purposes, to contrast it with other more
realistic algorithms.
10. Write a Java program to sort an array of positive integers using the Cocktail
sort Algorithm.
Cocktail shaker sort (also known as bidirectional bubble sort, cocktail sort,
shaker sort, ripple sort, shuffle sort, or shuttle sort ) is a variation of bubble
sort that is both a stable sorting algorithm and a comparison sort. The algorithm
differs from a bubble sort in that it sorts in both directions on each pass through
the list. This sorting algorithm is only marginally more difficult to implement
than a bubble sort and solves the problem of turtles in bubble sorts. It provides
only marginal performance improvements, and does not improve asymptotic
performance; like the bubble sort, it is not of practical interest, though it finds
some use in education.
11. Write a Java program to sort an array of given integers using the CombSort
Algorithm.
The Comb Sort is a variant of the Bubble Sort. Like the Shell sort, the Comb Sort
increases the gap used in comparisons and exchanges. Some implementations use the
insertion sort once the gap is less than a certain amount. The basic idea is to
eliminate turtles, or small values near the end of the list, since in a bubble sort
these slow the sorting down tremendously. Rabbits, large values around the
beginning of the list do not pose a problem in bubble sort.
12. Write a Java program to sort an array of given integers using CountingSort
Algorithm.
13. Write a Java program to sort an array of given integers using Gnome sort
Algorithm.
14. Write a Java program to sort an array of given integers using Pancake sort
Algorithm.
Pancake sorting is the colloquial term for the mathematical problem of sorting a
disordered stack of pancakes in order of size when a spatula can be inserted at any
point in the stack and used to flip all pancakes above it. A pancake number is the
minimum number of flips required for a given number of pancakes. The problem was
first discussed by American geometer Jacob E. Goodman. It is a variation of the
sorting problem in which the only allowed operation is to reverse the elements of
some prefix of the sequence.
15. Write a Java program to sort an array of given integers using Permutation sort
Algorithm.
16. Write a Java program to sort an array of given integers Shell Sort Algorithm.
17. Write a Java program to sort an array of given non-negative integers using
Sleep Sort Algorithm.
Sleep sort works by starting a separate task for each item to be sorted, where each
task sleeps for an interval corresponding to the item's sort key, then emits the
item. Items are then collected sequentially in time.
18. Write a Java program to sort an array of given non-negative integers using
Stooge Sort Algorithm.
19. Write a Java program to sort an array of given integers using Bucket Sort
Algorithm.