Skip to content

Instantly share code, notes, and snippets.

@copley
Created May 5, 2020 03:55
Show Gist options
  • Save copley/6423bd46a14913aac68b3db277d8c7b1 to your computer and use it in GitHub Desktop.
Save copley/6423bd46a14913aac68b3db277d8c7b1 to your computer and use it in GitHub Desktop.
2000 Java Problems
https://www.w3resource.com/java-exercises/sorting/index.php
Basic Part-I
Basic Part-II
Data Types
Conditional Statement
Array
String
Date Time
Method
Numbers
File Input-Output
Collection
Math
Sorting
Search
Basics
Java Basic Programming : Exercises, Practice, Solution
Last update on April 25 2020 05:30:20 (UTC/GMT +8 hours)
[An editor is available at the bottom of the page to write and execute the scripts.]
1. Write a Java program to print 'Hello' on screen and then print your name on a separate line.
Expected Output :
Hello
Alexandra Abramov
2. Write a Java program to print the sum of two numbers.
Test Data:
74 + 36
Expected Output :
110
3. Write a Java program to divide two numbers and print on the screen.
Test Data :
50/3
Expected Output :
16
4. Write a Java program to print the result of the following operations.
Test Data:
a. -5 + 8 * 6
b. (55+9) % 9
c. 20 + -3*5 / 8
d. 5 + 15 / 3 * 2 - 8 % 3
Expected Output :
43
1
19
13
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
8. Write a Java program to display the following pattern.
Sample Pattern :
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
10. Write a Java program to compute a specified formula.
Specified Formula :
4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))
Expected Output
2.9760461760461765
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
14. Write a Java program to print an American flag on the screen.
Expected Output
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
==============================================
==============================================
==============================================
==============================================
==============================================
==============================================
15. Write a Java program to swap two variables.
16. Write a Java program to print a face.
Expected Output
+"""""+
[| o o |]
| ^ |
| '-' |
+-----+
17. Write a Java program to add two binary numbers.
Input Data:
Input first binary number: 10
Input second binary number: 11
Expected Output
Sum of two binary numbers: 101
18. Write a Java program to multiply two binary numbers.
Input Data:
Input the first binary number: 10
Input the second binary number: 11
Expected Output
Product of two binary numbers: 110
19. Write a Java program to convert a decimal number to binary number.
Input Data:
Input a Decimal Number : 5
Expected Output
Binary number is: 101
20. Write a Java program to convert a decimal number to hexadecimal number.
Input Data:
Input a decimal number: 15
Expected Output
Hexadecimal number is : F
21. Write a Java program to convert a decimal number to octal number.
Input Data:
Input a Decimal Number: 15
Expected Output
Octal number is: 17
22. Write a Java program to convert a binary number to decimal number.
Input Data:
Input a binary number: 100
Expected Output
Decimal Number: 4
23. Write a Java program to convert a binary number to hexadecimal number.
Input Data:
Input a Binary Number: 1101
Expected Output
HexaDecimal value: D
24. Write a Java program to convert a binary number to a Octal number.
Input Data:
Input a Binary Number: 111
Expected Output
Octal number: 7
25. Write a Java program to convert a octal number to a decimal number.
Input Data:
Input any octal number: 10
Expected Output
Equivalent decimal number: 8
26. Write a Java program to convert a octal number to a binary number.
Input Data:
Input any octal number: 7
Expected Output
Equivalent binary number: 111
27. Write a Java program to convert a octal number to a hexadecimal number.
Input Data:
Input a octal number : 100
Expected Output
Equivalent hexadecimal number: 40
28. Write a Java program to convert a hexadecimal to a decimal number.
Input Data:
Input a hexadecimal number: 25
Expected Output
Equivalent decimal number is: 37
29. Write a Java program to convert a hexadecimal to a binary number.
Input Data:
Enter Hexadecimal Number : 37
Expected Output
Equivalent Binary Number is: 110111
30. Write a Java program to convert a hexadecimal to a octal number.
Input Data:
Input a hexadecimal number: 40
Expected Output
Equivalent of octal number is: 100
31. Write a Java program to check whether Java is installed on your computer.
Expected Output
Java Version: 1.8.0_71
Java Runtime Version: 1.8.0_71-b15
Java Home: /opt/jdk/jdk1.8.0_71/jre
Java Vendor: Oracle Corporation
Java Vendor URL: http://Java.oracle.com/
Java Class Path: .
32. Write a Java program to compare two numbers.
Input Data:
Input first integer: 25
Input second integer: 39
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
The sum of the digits is: 7
34. Write a Java program to compute the area of a hexagon.
Area of a hexagon = (6 * s^2)/(4*tan(π/6))
where s is the length of a side
Input Data:
Input the length of a side of the hexagon: 6
Expected Output
The area of the hexagon is: 93.53074360871938
35. Write a Java program to compute the area of a polygon.
Area of a polygon = (n*s^2)/(4*tan(π/n))
where n is n-sided polygon and s is the length of a side
Input Data:
Input the number of sides on the polygon: 7
Input the length of one of the sides: 6
Expected Output
The area is: 130.82084798405722
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
The distance between those points is: 1480.0848451069087 km
37. Write a Java program to reverse a string.
Input Data:
Input a string: The quick brown fox
Expected Output
Reverse string: xof nworb kciuq ehT
38. Write a Java program to count the letters, spaces, numbers and other characters of an input string.
Expected Output
The string is : Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33
letter: 23
space: 9
number: 10
other: 6
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
List of available character sets:
Big5
Big5-HKSCS
CESU-8
EUC-JP
EUC-KR
GB18030
GB2312
GBK
...
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
The ASCII value of Z is :90
42. Write a Java program to input and display your password.
Expected Output
Input your Password:
Your password was: abc@123
43. Write a Java program to print the following string in a specific format (see the output).
Sample Output
Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are
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
45. Write a Java program to find the size of a specified file.
Sample Output:
/home/students/abc.txt : 0 bytes
/home/students/test.txt : 0 bytes
46. Write a Java program to display the system time.
Sample Output:
Current Date time: Fri Jun 16 14:17:40 IST 2017
47. Write a Java program to display the current date time in specific format.
Sample Output:
Now: 2017/06/16 08:52:03.066
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,
51. Write a Java program to convert a string to an integer in Java.
Sample Output:
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:
Input the first number : 5
Input the second number: 10
Input the third number : 15
The result is: true
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:
Input the first number : 5
Input the second number: 10
Input the third number : 15
The result is: true
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:
Input the first number : 5
Input the second number: 10
Input the third number : 15
The result is: true
55. Write a Java program to convert seconds to hour, minute and seconds.
Sample Output:
Input seconds: 86399
23:59:59
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:
5
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.
59. Write a Java program to convert a given string into lowercase.
Sample Output:
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
61. Write a Java program to reverse a word.
Sample Output:
Input a word: dsaf
Reverse word: fasd
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:
Input the first number : 15
Input the second number: 20
Input the third number : 25
false
63. Write a Java program that accepts two integer values from the user and return the larger values. However if the two values are the same, return 0 and return the smaller value if the two values have the same remainder when divided by 6.
Sample Output:
Input the first number : 12
Input the second number: 13
Result: 13
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:
Input the first number : 35
Input the second number: 45
Result: true
65. Write a Java program to calculate the modules of two numbers without using any inbuilt modulus operator.
Sample Output:
Input the first number : 19
Input the second number: 7
5
66. Write a Java program to compute the sum of the first 100 prime numbers.
Sample Output:
Sum of the first 100 prime numbers: 24133
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:
Python Tutorial 3.0
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:
Array1: [50, -20, 0]
Array2: [5, -50, 10]
New Array: [50, 10]
78. Write a Java program to test that a given array of integers of length 2 contains a 4 or a 7.
Sample Output:
Original Array: [5, 7]
true
79. Write a Java program to rotate an array (length 3) of integers in left direction.
Sample Output:
Original Array: [20, 30, 40]
Rotated Array: [30, 40, 20]
80. Write a Java program to get the larger value between first and last element of an array (length 3) of integers .
Sample Output:
Original Array: [20, 30, 40]
Larger value between first and last element: 40
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:
Original Array: [20, 30, 40]
New array after swaping the first and last elements: [40, 30, 20]
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:
Original Array: [20, 30, 40, 50, 67]
Largest element between first, last, and middle values: 67
83. Write a Java program to multiply corresponding elements of two arrays of integers.
Sample Output:
Array1: [1, 3, -5, 4]
Array2: [1, 4, -5, -2]
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.
93. Write a Java program to test if an array of integers contains an element 10 next to 10 or an element 20 next to 20, but not both.
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.
96. Write a Java program to check if there is a 10 in a given array of integers with a 20 somewhere later in the array.
97. Write a Java program to check if an array of integers contains a specified number next to each other or there are two same specified numbers separated by one element.
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.
107. Write a Java program to check if an array of integers contains three increasing adjacent numbers.
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.
120. Write a Java program that searches a value in an m x n matrix.
121. Write a Java program to reverse a given linked list.
Example: For linked list 20->40->60->80, the reversed linked list is 80->60->40->20
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
128.Write a Java program to calculate the median of an given unsorted array of integers.
Example: {10,2,38,23,38,23,21}
Output: 23
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
143. Write a Java program to merge two given sorted lists.
Sample Output:
Merge Two Sorted ListsT:
1 2 3 7 9 13 40
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
149. Write a Java program to check if a given string is a permutation of another given string.
Sample Output:
Original strings: xxyz yxzx
true
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
Basics II
Java Basic Programming : Exercises, Practice, Solution
Last update on April 14 2020 12:36:31 (UTC/GMT +8 hours)
[An editor is available at the bottom of the page to write and execute the scripts.]
151. Write a Java program to find the value of specified expression.
a) 101 + 0) / 3
b) 3.0e-6 * 10000000.1
c) true && true
d) false && true
e) (false && false) || (true && true)
f) (false || false) && (true && 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.
Original Number: 2350
Result: 115
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]
166. Write a Java program to transform a given integer to String format.
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.
Original array: [0, 3, 4, 0, 1, 2, 5, 0]
Result: [3, 4, 1, 2, 5, 0, 0, 0]
168. Write a Java program to multiply two given integers without using the multiply operator(*).
Input the first number: 25
Input the second number: 5
Result: 125
169. Write a Java program to reverse the content of a sentence (assume a single space between two words) without reverse every word.
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.
Original array: [1, 1, 2, 3, 3, 4, 5, 2, 4, 5, 6, 7, 8, 9, 6, -1, -2]
7
171. Write a Java program to accept two string and test if the second string contains the first one.
Input first string: Once in a blue moon
Input second string: See eye to eye
If the second string contains the first one? false
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:
Original array: [1, 2, 3, 4, 5, 6, 7, 8, 8]
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:
Original array: [1, 2, 3, 4, 5, 6, 7, 8, 8]
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.
Sample Singly linked list: 10->20->30->40->50
Delete the fourth node i.e. 40
Result: 10->20->30->50
Expected Output:
Original Linked list:
10->20->30->40->50
After deleting the fourth node, Linked list becomes:
10->20->30->50
176. Write a Java program to partition an given array of integers into even number first and odd number second.
Expected Output
Original array: [7, 2, 4, 1, 3, 5, 6, 8, 2, 10]
After partition the said array becomes: [10, 2, 4, 2, 8, 6, 5, 3, 1, 7]
177. Write a Java program to get a new binary tree with same structure and same value of a given binary tree.
Expected Output:
Original Treenode:
4
5
2
3
1
Clone of the said 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:
Original array: [10, 11, 12, 13, 14, 7, 8, 9, 1, 2, 3]
Size of longest increasing continuous subsequence: 5
179. Write a Java program to plus one to the number of a given positive numbers represented as an array of digits.
Sample array: [9, 9, 9, 9] which represents 9999
Output: [1, 0, 0, 0, 0].
Expected Output:
Original array: [9, 9, 9, 9]
Array of digits: [1, 0, 0, 0, 0]
180. Write a Java program to swap every two adjacent nodes of a given linked list.
Expected Output:
Original Linked list:
10->20->30->40->50
After swiping Linked list becomes:
20->10->40->30->50
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:
Original String: The length of last word
Length of the last word of the above string: 4
182. Write a Java program to check if two binary trees are identical or not. Assume that two binary trees have the same structure and every identical position has the same value.
Sample Output:
Comparing TreeNode a and TreeNode b:
false
Comparing TreeNode b and TreeNode c:
true
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:
Input a positive integer: 25
7
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:
Length of the longest consecutive sequence path: 4
185. Write a Java program to check if two given strings are isomorphic or not.
Expected Output:
Is abca and zbxz are Isomorphic? true
186. Write a Java program to check if a number is a strobogrammatic number. The number is represented as a string.
According to Wikipedia "A strobogrammatic number is a number whose numeral is rotationally symmetric, so that it appears the same when rotated 180 degrees. In other words, the numeral looks the same right-side up and upside down (e.g., 69, 96, 1001). A strobogrammatic prime is a strobogrammatic number that is also a prime number, i.e., a number that is only divisible by one and itself (e.g., 11). It is a type of ambigram, words and numbers that retain their meaning when viewed from a different perspective, such as palindromes."
The first few strobogrammatic numbers are:
0, 1, 8, 11, 69, 88, 96, 101, 111, 181, 609, 619, 689, 808, 818, 888, 906, 916, 986, 1001, 1111, 1691, 1881, 1961, 6009, 6119, 6699, 6889, 6969, 8008, 8118, 8698, 8888, 8968, 9006, 9116, 9696, 9886, 9966, ...
Expected Output:
Is 9006 is Strobogrammatic? true
187. Write a Java program to find the index of first non-repeating character in a given string.
Expected Output:
Index of first non-repeating character in 'google' is: 4
188. Write a Java program to find all the start indices of a given string's anagrams in another given string.
Expected Output:
Original String: zyxwyxyxzwxyz
Starting anagram indices of xyz: [0, 6, 10]
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:
'123' + '456' = 579
190. Write a Java program to find the missing string from two given strings.
Expected Output:
Missing string: [Solution]
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:
Input a positive integer: 25
Is 25 sum of two square numbers? true
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:
Input a positive integer: 25
Sum of subsets of n is : 1157627904
194. Write a Java program to find the all positions of a given number in a given matrix. If the number not found print ("Number not found!").
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
197. Write a Java program to test if a given number (positive integer ) is a perfect square or not.
Expected Output:
Input a positive integer: 6
Is the said number perfect square? false
198. Write a Java program to get the position of a given prime number.
Expected Output:
Input a positive integer: 15
Position of the said Prime number: 6
199. Write a Java program to check a string follows a given pattern.
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:
Is the string and pattern matched? false
200. Write a Java program to remove duplicate letters and arrange in lexicographical order from a given string which contains only lowercase letters.
Note: In mathematics, the lexicographic or lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product) is a generalization of the way words are alphabetically ordered based on the alphabetical order of their component letters.
Expected Output:
Original string: zxywooxz
After removing duplicate characters: xywoz
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:
Original Array: [1, 3, 3, 5, 6, 6]
Target of subsets: 4
After removing duplicate characters: true
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:
Original Array: [4, 2, 3, 3, 7, 2, 4]
Value of k: 6
Total number of continuous subarrays: 3
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:
Original Array: [4, 2, 3, 3, 7, 2, 4]
Value of k: 3
Maximum average value: 4.333333333333333
204. Write a Java program to compute xn % y where x, y and n are all 32bit integers.
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:
Input number of elements of the array:
3
Input number format: 2 3 4 5:
Enter elements:
6 7 8
Enter target sum:
21
A solution set is:
{ 6 7 8 }
207. Write a Java program to merge two sorted (ascending) linked lists in ascending order.
Expected Output:
How many elements do you want to add in 1st linked list?: 3
Input numbers of 1st linked list in ascending order: 1 2 3
How many elements do you want to add in 2nd linked list?: 3
Input numbers of 2nd linked list in ascending order: 4 5 6
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:
Enter a string (you can include space as well)
aaaabbbbcccccddddeeee
The compressed string along with the counts of repeated characters is:
a4b4c5d4e4
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.
Input number of elements of the array:
3
Input number format: 2 3 4 5:
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:
0 ≤ height of building (integer) ≤ 10,000
Expected Output:
Input the heights of eight buildings:
25 19 23 45 18 23 24 19
Heights of the top three buildings:
45
25
24
212. Write a Java program to compute the digit number of sum of two given integers.
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 two integers(a b):
13 25
Digit number of sum of said two integers:
2
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 three integers(sides of a triangle)
6 9 12
If the given sides form a right triangle?
No
214. Write a Java program which solve the equation:
ax+by=c
dx+ey=f
Print the values of x, y where a, b, c, d, e and f are given.
Input:
a,b,c,d,e,f separated by a single space.
(-1,000 ≤ a,b,c,d,e,f ≤ 1,000)
Expected Output:
Input the value of a, b, c, d, e, f:
5 6 8 9 7 4
-1.684 2.737
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:
a,b,c,d,e,f separated by a single space.
(-1,000 ≤ a,b,c,d,e,f ≤ 1,000)
Expected Output:
Input the number(n):
5
Number of combinations of a, b, c and d :
56
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:
Input the number(n):
1235
Number of prime numbers which are less than or equal to n.:
202
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:
x1, y1, x2, y2, x3, y3 separated by a single space.
Expected Output:
Input x1, y1, x2, y2, x3, y3 separated by a single space:
5 6 4 8 7 9
Radius and the central coordinate:
1.821 (5.786 7.643)
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:
Input (x1, y1)
2
6
Input (x2, y2)
3
5
Input (x3, y3)
4
6
Input (xp, yp)
5
6
The point is outside the triangle.
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:
Input two integers:
25
46
Sum of the said two integers:
71
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:
Input six integers:
4 6 8 2 7 9
After sorting the said integers:
9 8 7 6 4 2
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:
−100 ≤ x1, y1, x2, y2, x3, y3, x4, y4 ≤ 100
Each value is a real number with at most 5 digits after the decimal point.
Expected Output:
Input P(x1,y1),separated by a space.
5 6
Input Q(x2,y2),separated by a space.
4 2
Input R(x3,y3),separated by a space.
5 3
Input S(x4,y4),separated by a space.
5 6
Two lines are not parallel.
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:
You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000.
Input numbers are separated by a space.
Input 0 to exit.
Expected Output:
How many integers would you like to input?
5
Input the integers:
25 61 35 42 66
Maximum sum of the said contiguous subsequence:
229
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:
Input numbers (real numbers) are separated by a space.
Expected Output:
Input x1, y1, r1: (numbers are separated by a space)
5 6 8 7
Input x2, y2, r2: (numbers are separated by a space)
8 9 5 4
C1 and C2 do not overlap
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:
Input the month(1-12)
9
Input date (1-31)
15
Name of the date:
Wednesday
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:
A sequence of integer’s ai (1 ≤ ai ≤ 100). The number of integers is less than or equals to 100.
Expected Output:
How many integers would you like to input(Max.100?)
5
Input the integers:
25
35
15
5
45
Mode value(s)in ascending order:
5
15
25
35
45
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:
A sequence of integer’s ai (1 ≤ ai ≤ 100). The number of integers is less than or equals to 100.
Expected Output:
Thank you for your comment and your participation.
Input a text in a line:
Most frequent text and the word which has the maximum number of letters:
your participation.
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:
Input number of combinations and sum (separated by a space in a line):
3 6
Number of combinations:
3
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:
Two adjoined sides and the diagonal.
1 ≤ ai, bi, ci ≤ 1000, ai + bi > ci
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:
English letters (including single byte alphanumeric characters, blanks, symbols) are given on one line. The length of the input character string is 1000 or less.
Output:
Exchanged character string of python and java on one line.
Expected Output:
Input the string:
python is more propular than java
New string:
java is more propular than python
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:
Data is a sequence of 8 numbers (numbers from 0 to 9).
Output:
The difference between the largest integer and the smallest integer.
Sample Output:
Input an integer created by 8 numbers from 0 to 9:
567894321
Difference between the largest and the smallest integer from the given integer:
75308643
232. Write a Java program to compute the sum of first n given prime numbers.
Input:
n ( n ≤ 10000). Input 0 to exit the program.
Sample Output:
Input a number (n<=10000) to compute the sum:
100
Sum of first 100 prime numbers:
24133
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:
Input an even number: 100
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.
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:
Input number of straight lines:
5
Number of regions:
16
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:
Input xp, yp, xq, yq, xr, yr, xs, ys:
3.5 4.5 2.5 -1.5 3.5 1.0 0.0 4.5
Two lines are not orthogonal.
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:
Input some text and numeric values:
5 apple and 10 orange are rotten in the basket
Sum of the numeric values:
15
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 10 rows of 10 numbers representing green squares (island) as 1 and blue squares (sea) as zeros
1100000111
1000000111
0000000111
0010001000
0000011100
0000111110
0001111111
1000111110
1100011100
1110001000
Number of islands:
5
238. When character are consecutive in a string , it is possible to shorten the character string by replacing the character with a certain rule. For example, in the case of the character string YYYYY, if it is expressed as # 5 Y, it is compressed by one character.
Write a Java program to restore the original string by entering the compressed string with this rule. However, the # character does not appear in the restored character string.
Note: The original sentences are uppercase letters, lowercase letters, numbers, symbols, less than 100 letters, and consecutive letters are not more than 9 letters.
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:
Input the text:
XY#6Z1#4023
XYZZZZZZ1000023
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 sentence (1024 characters. max.)
The quick brown fox
3 to 6 characters length of words:
The quick brown fox
240. Arrange integers (0 to 99) as narrow hilltop, as illustrated in Figure 1. Reading such data representing huge, when starting from the top and proceeding according to the next rule to the bottom.
Write a Java program that compute the maximum value of the sum of the passing integers.
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:
Input the numbers (ctrl+c to exit):
8
4,9
9,2,1
3,8,5,5
5,6,3,7,6
3,8,5,5
9,2,1
4,9
8
Maximum value of the sum of integers passing according to the rule on one line.
64
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:
Input a positive integer:
252
Number of combinations of a,b,c,d:
2731135
242. Your task is to develop a small part of spreadsheet software. Write a Java program which adds up columns and rows of given table as shown in the specified figure:
Input number of rows/columns (0 to exit)
4
25 69 51 26
68 35 29 54
54 57 45 63
61 68 47 59
Result:
25 69 51 26 171
68 35 29 54 186
54 57 45 63 219
61 68 47 59 235
208 229 172 202 811
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
Word and page number in alphabetical order:
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.
The conditions for getting the "correct answer" are:
a) There must be only three characters X, Y, and Z in the string, and no other characters.
b) Any string of any form such as aXYZa can get the "correct answer", where a is either an empty string or a string consisting only of the letter X;
c) If aXbZc is correct, aXbYZca is also correct, where a, b, c are either empty strings or a string consisting only of the letter X.
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
Input a positive number(max three digits):
235
Result:
HHTTT12345
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
248. From Wikipedia, An abecedarium (or abecedary) is an inscription consisting of the letters of an alphabet, almost always listed in order. Typically, abecedaria (or abecedaries) are practice exercises.
Write a Java program to check if each letter of a given word (Abecadrian word) is less than the one before it.
Input:
Input a word: ABCD
Is Abecadrian word? true
249. From Wikipedia,
The Hamming weight of a string is the number of symbols that are different from the zero-symbol of the alphabet used. It is thus equivalent to the Hamming distance from the all-zero string of the same length. For the most typical case, a string of bits, this is the number of 1's in the string, or the digit sum of the binary representation of a given number and the ℓ₁ norm of a bit vector. In this binary case, it is also called the population count, popcount, sideways sum, or bit summation.
Example:
String Hamming weight
11101 4
11101000 4
00000000 0
789012340567 10
Write a Java program to count the number of set bits in a 32-bit integer.
Input:
Input a number: 1427
6
250. From Wikipedia,
A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data. Blocks of data entering these systems get a short check value attached, based on the remainder of a polynomial division of their contents. On retrieval, the calculation is repeated and, in the event the check values do not match, corrective action can be taken against data corruption. CRCs can be used for error correction.
Example:
Write a Java program to generate a crc32 checksum of a given string or byte array.
Input:
Input a string: The quick brown fox
crc32 checksum of the string: b74574de
Data Types
Java Data Types: Exercises, Practice, Solution
Last update on February 26 2020 08:08:12 (UTC/GMT +8 hours)
[An editor is available at the bottom of the page to write and execute the scripts.]
1. Write a Java program to convert temperature from Fahrenheit to Celsius degree.
Test Data
Input a degree in Fahrenheit: 212
Expected Output:
212.0 degree Fahrenheit is equal to 100.0 in Celsius
2. Write a Java program that reads a number in inches, converts it to meters.
Note: One inch is 0.0254 meter.
Test Data
Input a value for inch: 1000
Expected Output :
1000.0 inch is 25.4 meters
3. Write a Java program that reads an integer between 0 and 1000 and adds all the digits in the integer.
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
5. Write a Java program that prints the current time in GMT.
Test Data
Input the time zone offset to GMT: 256
Expected Output:
Current time is 23:40:24
6. Write a Java program to compute body mass index (BMI).
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.
Conditional Statements
Java Conditional Statement : Exercises, Practice, Solution
Last update on February 26 2020 08:08:14 (UTC/GMT +8 hours)
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
7. Write a Java program to find the number of days in a month.
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
10. Write a program in Java to display the first 10 natural numbers.
Expected Output :
The first 10 natural numbers are:
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 :
Input the 5 numbers :
1
2
3
4
5
The sum of 5 no is : 15
The Average is : 3.0
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 :
Number is : 1 and cube of 1 is : 1
Number is : 2 and cube of 2 is : 8
Number is : 3 and cube of 3 is : 27
Number is : 4 and cube of 4 is : 64
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 :
The odd numbers are :
1
3
5
7
9
The Sum of odd Natural Number upto 5 terms is: 25
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
20. Write a program in Java to print the Floyd's Triangle.
Test Data
Input number of rows : 5
Expected Output :
1
01
101
0101
10101
21. Write a program in Java to display the pattern like a diamond.
Test Data
Input number of rows (half of the diamond) : 7
Expected Output :
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
22. Write a Java program to display Pascal's triangle.
Test Data
Input number of rows: 5
Expected Output :
Input number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
23. Write a java program to generate a following *'s triangle.
Test Data
Input the number: 6
Expected Output :
******
*****
****
***
**
*
24. Write a java program to generate a following @'s triangle.
Test Data
Input the number: 6
Expected Output :
@
@@
@@@
@@@@
@@@@@
@@@@@@
25. Write a Java program to display the number rhombus structure.
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 :
Number of digits in the number: 6
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 :
All numbers are different
31. Write a program that accepts three numbers from the user and prints "increasing" if the numbers are in increasing order, "decreasing" if the numbers are in decreasing order, and "Neither increasing or decreasing order" otherwise.
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 floating­point numbers and checks whether they are the same up to two decimal places.
Test Data
Input first floating­point number: 1235
Input second floating­point number: 2534
Expected Output :
These numbers are different.
Array
Java Array: Exercises, Practice, Solution
Last update on April 29 2020 12:40:24 (UTC/GMT +8 hours)
1. Write a Java program to sort a numeric array and a string array.
2. Write a Java program to sum values of an array.
3. Write a Java program to print the following grid.
Expected Output :
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
4. Write a Java program to calculate the average value of array elements.
5. Write a Java program to test if an array contains a specific value.
6. Write a Java program to find the index of an array element.
7. Write a Java program to remove a specific element from an array.
8. Write a Java program to copy an array by iterating the array.
9. Write a Java program to insert an element (specific position) into an array.
10. Write a Java program to find the maximum and minimum value of an array.
11. Write a Java program to reverse an array of integer values.
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.
16. Write a Java program to remove duplicate elements from an array.
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.
21. Write a Java program to convert an ArrayList to an array.
22. Write a Java program to find all pairs of elements in an array whose sum is equal to a specified number.
23. Write a Java program to test the equality of two arrays.
24. Write a Java program to find a missing number in an array.
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 a 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 a given array whose sum is equal to a given value.
44. Write a Java program to count the number of possible triangles from a 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 a given array of integers where all negative integers appear before all the positive integers.
49. Write a Java program to arrange the elements of a 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 a 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 a 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 a given array of integers.
54. Write a Java program to check if a given array contains a subarray with 0 sum.
Example:
Input :
nums1= { 1, 2, -2, 3, 4, 5, 6 }
nums2 = { 1, 2, 3, 4, 5, 6 }
nums3 = { 1, 2, -3, 4, 5, 6 }
Output:
Does the said array contain a subarray with 0 sum: true
Does the said array contain a subarray with 0 sum: false
Does the said array contain a subarray with 0 sum: true
55. Write a Java program to print all sub-arrays with 0 sum present in a given array of integers.
Example:
Input :
nums1 = { 1, 3, -7, 3, 2, 3, 1, -3, -2, -2 }
nums2 = { 1, 2, -3, 4, 5, 6 }
nums3= { 1, 2, -2, 3, 4, 5, 6 }
Output:
Sub-arrays with 0 sum : [1, 3, -7, 3]
Sub-arrays with 0 sum : [3, -7, 3, 2, 3, 1, -3, -2]
Sub-arrays with 0 sum : [1, 2, -3]
Sub-arrays with 0 sum : [2, -2]
56. Write a Java program to sort a given binary array in linear times.
From Wikipedia,
Linear time: An algorithm is said to take linear time, or O(n) time, if its time complexity is O(n). Informally, this means that the running time increases at most linearly with the size of the input. More precisely, this means that there is a constant c such that the running time is at most cn for every input of size n. For example, a procedure that adds up all elements of a list requires time proportional to the length of the list, if the adding time is constant, or, at least, bounded by a constant.
Linear time is the best possible time complexity in situations where the algorithm has to sequentially read its entire input. Therefore, much research has been invested into discovering algorithms exhibiting linear time or, at least, nearly linear time. This research includes both software and hardware methods. There are several hardware technologies which exploit parallelism to provide this. An example is content-addressable memory. This concept of linear time is used in string matching algorithms such as the Boyer–Moore algorithm and Ukkonen's algorithm.
Example:
Input :
b_nums[] = { 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 }
Output:
After sorting: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
57. Write a Java program to check if a sub-array is formed by consecutive integers from a given array of integers.
Example:
Input :
nums = { 2, 5, 0, 2, 1, 4, 3, 6, 1, 0 }
Output:
The largest sub-array is [1, 7]
Elements of the sub-array: 5 0 2 1 4 3 6
58. Given two sorted arrays A and B of size p and q, write a Java program to merge elements of A with B by maintaining the sorted order i.e. fill A with first p smallest elements and fill B with remaining elements.
Example:
Input :
int[] A = { 1, 5, 6, 7, 8, 10 }
int[] B = { 2, 4, 9 }
Output:
Sorted Arrays:
A: [1, 2, 4, 5, 6, 7]
B: [8, 9, 10]
59. Write a Java program to find maximum product of two integers in a given array of integers.
Example:
Input :
nums = { 2, 3, 5, 7, -7, 5, 8, -5 }
Output:
Pair is (7, 8), Maximum Product: 56
60. Write a Java program to shuffle a given array of integers.
Example:
Input :
nums = { 1, 2, 3, 4, 5, 6 }
Output:
Shuffle Array: [4, 2, 6, 5, 1, 3]
61. Write a Java program to rearrange a given array of unique elements such that every second element of the array is greater than its left and right elements.
Example:
Input :
nums= { 1, 2, 4, 9, 5, 3, 8, 7, 10, 12, 14 }
Output:
Array with every second element is greater than its left and right elements:
[1, 4, 2, 9, 3, 8, 5, 10, 7, 14, 12]
62. Write a Java program to find the equilibrium indices from a given array of integers.
Example:
Input :
nums = {-7, 1, 5, 2, -4, 3, 0}
Output:
Equilibrium indices found at : 3
Equilibrium indices found at : 6
63. Write a Java program to replace each element of the array with product of every other element in a given array of integers.
Example:
Input :
nums1 = { 1, 2, 3, 4, 5, 6, 7}
nums2 = {0, 1, 2, 3, 4, 5, 6, 7}
Output:
Array with product of every other element:
[5040, 2520, 1680, 1260, 1008, 840, 720]
Array with product of every other element:
[5040, 0, 0, 0, 0, 0, 0, 0]
64. Write a Java program to find Longest Bitonic Subarray in a given array.
A bitonic subarray is a subarray of a given array where elements are first sorted in increasing order, then in decreasing order. A strictly increasing or strictly decreasing subarray is also accepted as bitonic subarray.
Example:
Input :
nums = { 4, 5, 9, 5, 6, 10, 11, 9, 6, 4, 5 }
Output:
The longest bitonic subarray is [3,9]
Elements of the said sub-array: 5 6 10 11 9 6 4
The length of longest bitonic subarray is 7
65. Write a Java program to find maximum difference between two elements in a given array of integers such that smaller element appears before larger element.
Example:
Input :
nums = { 2, 3, 1, 7, 9, 5, 11, 3, 5 }
Output:
The maximum difference between two elements of the said array elements
10
66. Write a Java program to find contiguous subarray within a given array of integers which has the largest sum.
In computer science, the maximum sum subarray problem is the task of finding a contiguous subarray with the largest sum, within a given one-dimensional array A[1...n] of numbers. Formally, the task is to find indices and with, such that the sum is as large as possible.
Example:
Input :
int[] A = {1, 2, -3, -4, 0, 6, 7, 8, 9}
Output:
The largest sum of contiguous sub-array: 30
67. Write a Java program to find subarray which has the largest sum in a given circular array of integers.
Example:
Input :
nums1 = { 2, 1, -5, 4, -3, 1, -3, 4, -1 }
nums2 = { 1, -2, 3, 0, 7, 8, 1, 2, -3 }
Output:
The sum of subarray with the largest sum is 6
The sum of subarray with the largest sum is 21
68. Write a Java program to create all possible permutations of a given array of distinct integers.
Example:
Input :
nums1 = {1, 2, 3, 4}
nums2 = {1, 2, 3}
Output:
Possible permutations of the said array:
[1, 2, 3, 4]
[1, 2, 4, 3]
....
[4, 1, 3, 2]
[4, 1, 2, 3]
Possible permutations of the said array:
[1, 2, 3]
[1, 3, 2]
...
[3, 2, 1]
[3, 1, 2]
69. Write a Java program to find minimum subarray sum of specified size in a given array of integers.
Example:
Input :
nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10}
Output:
Sub-array size: 4
Sub-array from 0 to 3 and sum is: 10
70. Write a Java program to find the smallest length of a contiguous subarray of which the sum is greater than or equal to specified value. Return 0 instead.
Example:
Input :
nums = {1, 2, 3, 4, 6}
Output:
Minimum length of a contiguous subarray of which the sum is 8, 2
71. Write a Java program to form the largest number from a given list of non negative integers.
Example:
Input :
nums = {1, 2, 3, 0, 4, 6}
Output:
Largest number using the said array numbers: 643210
72. Write a Java program to find and print one continuous subarray (from a given array of integers) that if you only sort the said subarray in ascending order then the entire array will be sorted in ascending order.
Example:
Input :
nums1 = {1, 2, 3, 0, 4, 6}
nums2 = { 1, 3, 2, 7, 5, 6, 4, 8}
Output:
Continuous subarray:
1 2 3 0
Continuous subarray:
3 2 7 5 6 4
73. Write a Java program to sort a given array of distinct integers where all its numbers are sorted except two numbers.
Example:
Input :
nums1 = { 3, 5, 6, 9, 8, 7 }
nums2 = { 5, 0, 1, 2, 3, 4, -2 }
Output:
After sorting new array becomes: [3, 5, 6, 7, 8, 9]
After sorting new array becomes: [-2, 0, 1, 2, 3, 4, 5]
74. Write a Java program to find all triplets equal to a given sum in a unsorted array of integers.
Example:
Input :
nums = { 1, 6, 3, 0, 8, 4, 1, 7 }
Output:
Triplets of sum 7
(0 1 6)
(0 3 4)
String Exercises
Java String: Exercises, Practice, Solution
Last update on February 26 2020 08:08:09 (UTC/GMT +8 hours)
1. Write a Java program to get the character at the given index within the String.
Sample Output:
Original String = Java Exercises!
The character at position 0 is J
The character at position 10 is i
2. Write a Java program to get the character (Unicode code point) at the given index within the String.
Sample Output:
Original String : w3resource.com
Character(unicode point) = 51
Character(unicode point) = 101
3. Write a Java program to get the character (Unicode code point) before the specified index within the String.
Sample Output:
Original String : w3resource.com
Character(unicode point) = 119
Character(unicode point) = 99
4. Write a java program to count a number of Unicode code points in the specified text range of a String.
Sample Output:
Original String : w3rsource.com
Codepoint count = 9
5. Write a java program to compare two strings lexicographically.
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"
6. Write a java program to compare two strings lexicographically, ignoring case differences.
Sample Output:
String 1: This is exercise 1
String 2: This is Exercise 1
"This is exercise 1" is equal to "This is Exercise 1"
7. Write a Java program to concatenate a given string to the end of another string.
Sample Output:
String 1: PHP Exercises and
String 2: Python Exercises
The concatenated string: PHP Exercises and Python Exercises
8. Write a Java program to test if a given string contains the specified sequence of char values.
Sample Output:
Original String: PHP Exercises and Python Exercises
Specified sequence of char values: and
true
9. Write a Java program to compare a given string to the specified character sequence.
Sample Output:
Comparing example.com and example.com: true
Comparing Example.com and example.com: false
10. Write a Java program to compare a given string to the specified string buffer.
Sample Output:
Comparing example.com and example.com: true
Comparing Example.com and example.com: false
11. Write a Java program to create a new String object with the contents of a character array.
Sample Output:
The book contains 234 pages.
12. Write a Java program to check whether a given string ends with the contents of another string.
Sample Output:
"Python Exercises" ends with "se"? false
"Python Exercise" ends with "se"? true
13. Write a Java program to check whether two String objects contain the same data.
Sample Output:
"Stephen Edwin King" equals "Walter Winchell"? false
"Stephen Edwin King" equals "Mike Royko"? false
14. Write a Java program to compare a given string to another string, ignoring case considerations.
Sample Output:
"Stephen Edwin King" equals "Walter Winchell"? false
"Stephen Edwin King" equals "stephen edwin king"? true
15. Write a java program to print current date and time in the specified format.
Sample Output:
Current Date and Time :
June 19, 2017
3:13 pm
N.B. : The current date and time will change according to your system date and time.
16. Write a Java program to get the contents of a given string as a byte array.
Sample Output:
The new String equals This is a sample String.
17. Write a Java program to get the contents of a given string as a character array.
Sample Output:
The char array equals "[C@2a139a55"
18. Write a Java program to create a unique identifier of a given string.
Sample Output:
The hash for Python Exercises. is 863132599
19. Write a Java program to get the index of all the characters of the alphabet.
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:
str1 == str2? false
str1 == str3? true
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."
22. Write a java program to get the length of a given string.
Sample Output:
The string length of 'example.com' is: 11
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."
In the above string replace all the fox with cat.
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:
Red is favorite color. starts with Red? true
Orange is also my favorite color. starts with Red? false
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:
Original String: The Quick BroWn FoX!
String in lowercase: the quick brown fox!
30. Write a Java program to convert all the characters in a string to uppercase.
Sample Output:
Original String: The Quick BroWn FoX!
String in uppercase: THE QUICK BROWN FOX!
31. Write a Java program to trim any leading or trailing whitespace from a given string.
Sample Output:
Original String: Java Exercises
New String: Java Exercises
32. Write a Java program to find longest Palindromic Substring within a string.
Sample Output:
The given string is: thequickbrownfoxxofnworbquickthe
The longest palindrome substring in the giv
en string is; brownfoxxofnworb
The length of the palindromic substring is: 16
33. Write a Java program to find all interleavings of given strings.
Sample Output:
The given strings are: WX YZ
The interleavings strings are:
YWZX
WYZX
YWXZ
WXYZ
YZWX
WYXZ
34. Write a Java program to find the second most frequent character in a given string.
Sample Output:
The given string is: successes
The second most frequent char in the string is: c
35. Write a Java program to print all permutations of a given string with repetition.
Sample Output:
The given string is: PQR
The permuted strings are:
PPP
PPQ
PPR
...
RRP
RRQ
RRR
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:
The given string is: PMQNO
The interleaving strings are MNO and PQ
The given string is interleaving: true
The given string is: PNQMO
The interleaving strings are MNO and PQ
The given string is interleaving: false
37. Write a Java program to find Length of the longest substring without repeating characters.
Sample Output:
Input String : pickoutthelongestsubstring
The longest substring : [u, b, s, t, r, i, n, g]
The longest Substring Length : 8
38. Write a Java program to print after removing duplicates from a given string.
Sample Output:
The given string is: w3resource
After removing duplicates characters the new string is: w3resouc
39. Write a Java program to find first non repeating character in a string.
Sample Output:
The given string is: gibblegabbler
The first non repeated character in String is: i
40. Write a Java program to divide a string in n equal parts.
Sample Output:
The given string is: abcdefghijklmnopqrstuvwxy
The string divided into 5 parts and they are:
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:
The given string is: the quick brown fox
The given mask string is: queen
The new string is:
th ick brow fox
42. Write a Java program to print list items containing all characters of a given word.
Sample Output:
The given strings are: rabbit bribe dog
The given word is: bib
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:
The given string is: test string
Max occurring character in the given string is: t
44. Write a Java program to reverse a string using recursion.
Sample Output:
The given string is: The quick brown fox jumps
The string in reverse order is:
spmuj xof nworb kciuq ehT
45. Write a Java program to reverse words in a given string.
Sample Output:
The given string is: Reverse words in a given string
The new string after reversed the words: string given a in words Reverse
46. Write a Java program to reverse every word in a string using methods.
Sample Output:
The given string is: This is a test string
The string reversed word by word is:
sihT si a tset gnirts
47. Write a Java program to rearrange a string so that all same characters become d distance away.
Sample Output:
The given string is: accessories
The string after arrange newly is:
secrsecisao
48. Write a Java program to remove "b" and "ac" from a given string.
Sample Output:
The given string is: abrambabasc
After removing the new string is: aramaasc
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
50. Write a Java program to find lexicographic rank of a given string.
Sample Output:
The Given String is: BDCA
The Lexicographic rank of the given string is: 12
N.B.: Total possible permutations of BDCA are(lexicographic order) :
ABCD ABDC ACBD ACDB ADBC ADCB BACD BADC BCAD BCDA BDAC BDCA
1   2   3   4   5   6   7   8   9   10   11   12
The BDCA appear in 12 position of permutation (lexicographic order).
51. Write a Java program to count and print all the duplicates in the input string.
Sample Output:
The given string is: w3resource
The duplicate characters and counts are:
e appears 2 times
r appears 2 times
52. Write a Java program to check if two given strings are rotations of each other.
Sample Output:
The given strings are: ABACD and CDABA
The concatination of 1st string twice is: ABACDABACD
The 2nd string CDABA exists in the new string.
Strings are rotations of each other
53. Write a Java program to match two strings where one string contains wildcard characters.
Sample Output:
The given string is: abcdhgh
The given pattern string is: abc*d?*
The given pattern is matching.
54. Write a Java program to find the smallest window in a string containing all characters of another string.
Sample Output:
The given string is: welcome to w3resource
Characters to find in the main sring are: tower
The smallest window which contains the finding characters is : to w3re
55. Write a Java program to remove all adjacent duplicates recursively from a given string.
Sample Output:
The given string is: aabaarbarccrabmq
The new string after removing all adjacent duplicates is:
brmq
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:
The given strings are: food and door
The string after concatination are: foodoor
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:
The given strings is: string
The string after swap last two characters are: strign
58. Write a Java program to read a string and return true if it ends in "ng".
Sample Output:
The given strings is: string
The string containing ng at last: true
The given strings is: strign
The string containing ng at last: false
59. Write a Java program to read a string,if the string begins with &quot;red&quot; or &quot;black&quot; return that color string, otherwise return the empty string.
Sample Output:
The given strings is: blacksea
The string begins with the color: black
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:
The given strings is: Welcome and home
The new string is: comehome
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:
The given strings is: Welcome
The given numbers is: 3
The new string is: Welome
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:
The given strings is: goodboy
The 'good' appear in the string is: true
63. Write a Java program to return true from a given string if the first two characters in the string also appear at the end.
Sample Output:
The given strings is: educated
The first two characters appear in the last is: true
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:
The given strings is: educated
The new string is: ucated
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:
The given strings is: testcricket
The new string is: estcricke
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:
The given strings is: goat
The new string is: gat
he given strings is: photo
The new string is: hoto
The given strings is: ghost
The new string is: ghost
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:
The given strings is: oocyte
The new string is: cyte
The given strings is: boat
The new string is: bat
The given strings is: own
The new string is: wn
68. Write a Java program to read a string and returns after remove the # and its immediate left and right characters.
Sample Output:
The given strings is: test#string
The new string is: testring
The given strings is: test##string
The new string is: testring
The given strings is: test#the#string
The new string is: teshtring
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:
The given strings is: sweettoastbuttertoast
The new string is: butter
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:
The given strings is: gfpmpnppqab
The string is pq-balanced? true
The given strings is: gfpmpnpqpab
The string is pq-balanced? false
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:
The given strings are: xyz and pqrxyz
Is one string appears at the end of other? true
The given strings are: pqrxyz and xyz
Is one string appears at the end of other? true
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:
The given string is: dikchapop
Is p?p appear in the given string? true
The given string is: dikp$pdik
Is p?p appear in the given string? true
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:
The given strings is: testabc.test
Is 'abc' appear before period? true
The given string is: test.abctest
Is 'abc' appear before period? false
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:
The given strings is: MrsJemsmrsam
The prefix string length is: 3
Is 'Mrs' appear else where in the string? false
The given string is: MrsJemsMrsam
The prefix string length is: 3
Is 'Mrs' appear else where in the string? true
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:
The given string is: xxxabcxxxxx
Is abc appear in middle? false
The given string is: xxabcxxx
Is abc appear in middle? true
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:
The given string is: liveonwildlife
The substring life or li?e appear number of times: 2
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:
The given string is: string
The new string after repetition: inginging
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:
The given string is: zzgkitandkatcaketoket
The new string is: zgkitandkatcaketoket
80. Write a Java program to check whether the character immediately before and after of # is same in a given string.
Sample Output:
The given string is: moon#night
The before and after character are same: true
The given string is: bat##ball
The before and after character are same: false
The given string is: #moon#night
The before and after character are same: true
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:
The given string is: redcapmanwithbluecar
The appearance of red and blue are same: true
82. Write a Java program to repeat every character twice in the original string.
Sample Output:
The given string is: welcome
The new string is: wweellccoommee
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:
The given strings are: welcome and w3resource
The new string is: wwe3lrceosmoeurce
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:
The given string is: welcome
Number of repetition characters and repetition: 4
The new string is: welcwelwew
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:
The given string are: weablcoabmeab and ab
The new string is: elome
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:
The given string is: welllcommmmeee
The number of triples in the string is: 4
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:
The given string is: azzlea
Is z happy in the string: true
The given string is: azmzlea
Is z happy in the string: falses
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:
The given string is: it is a string
The new string is: it is not a string
89. Write a Java program to return the sum of the numbers (may form more than one digits), appearing in the string.
Sample Output:
The given string is: it 15 is25 a 20string
The sum of numbers in the string is: 60
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:
The given string is: Thisisthethesis
Are the appearance of 'the' and 'is' equal? false
91. Write a Java program to count the number of words ending in 'm' or 'n' (not case sensitive).
Sample Output:
The given string is: mam is in the room
The number of words ends eith m or n is: 3
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:
The main string is: This is the test string
The removable string is: st
The new string is: This is the te ring
93. Write a Java program to find the longest substring appears at both ends of a given string.
Sample Output:
The given string is: playersplay
The longest substring in the string is: play
94. Write a Java program to find the longest mirror image string at the both ends of a given string.
Sample Output:
The given string is: rotavator
The longest mirror image string in the string is: rotavator
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:
The given string is: zebrazone
The new string is: zebraone
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:
The given string is: w3resource.com
The new string is: w3rour.co
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:
The given string is: fizzez
Is 'z' appear twice respectively? true
99. Write a Java program to return a new string using every characters of even positions from a given string.
Sample Output:
The given string is: w3resource.com
The new string is: wrsuc.o
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.
Is 'million' present in the said text?
true
Is 'millions' present in the said text?
false
101. Write a Java program to test if a given string contains only digits.
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:
Convert String to int/Integer:
"1323" as int is 1323 and as Integer is 1323
Convert String to long/Long:
"13625478965325" as long is 13625478965325 and as Long is 13625478965325
Convert String to float/Float:
"25.135F" as float is 25.135 and as Float is 25.135
Convert String to double/Double:
"21.25478254D" as double is 21.25478254 and as Double is 21.25478254
false
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:
aa' has occured 3 times in 'abcd abc aabc baa abcaa'
106. Write a Java program to concatenate a given string with itself of a given number of times.
Sample Output:
Original string: PHP
After repeating 7 times: PHPPHPPHPPHPPHPPHPPHP
107. Write a Java program to counts occurrences of a certain character in a given string.
Date Time exercises
Java Date, Calendar and Time: Exercises, Practice, Solution
Last update on February 26 2020 08:08:13 (UTC/GMT +8 hours)
Java Date, Time and Calendar Exercises [44 exercises with solution]
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.
5. Write a Java program to get the current time in New York.
6. Write a Java program to get current full date and time.
7. Write a Java program to get the last day of the current month.
8. Write a Java program to get the last date of the 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.
11. Write a Java program to get the number of days of a month.
12. Write a Java program to get localized day-in-week name.
13. Write a Java program to get a day of the week of a specific date.
14. Write a Java program to get the current local time.
15. Write a Java program to add some hours to the current time.
16. Write a Java program to get a date after 2 weeks.
17. Write a Java program to get a date before and after 1 year compares to the current date.
18. Write a Java program to check a year is a leap year or not.
19. Write a Java program to get year and months between two dates.
20. Write a Java program to get current timestamp.
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 :
Default format of LocalDate=2016-09-16
16::Sep::2016
Default format of LocalDateTime=2016-09-16T11:46:01.455
16::Sep::2016 11::46::01
Default format of Instant=2016-09-16T06:16:01.456Z
Default format after parsing = 2014-04-27T21:39:48
25. Write a Java program to get the information of current/given year.
Sample format :
Sample Output :
Current Year: 2001
Is current year leap year? false
Length of the year: 365 days
26. Write a Java program to get the information of current/given month.
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
27. Write a Java program to get the information of a given time.
28. Write a Java program to display the date time information before some hours and minutes from current date time.
29. Write a Java program to convert a string to date.
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).
32. Write a Java program to calculate your age.
33. Write a Java program to get the next and previous Friday.
34. Write a Java program to get today's date at midnight time.
35. Write a Java program to extract date, time from the date string.
36. Write a Java program to convert a unix timestamp to date in Java.
37. Write a Java program to find seconds since 1970.
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.
44. Write a Java program to define and extract zone offsets.
Java Method Exercises
Java Methods: Exercises, Practice, Solution
Last update on February 26 2020 08:08:14 (UTC/GMT +8 hours)
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 smallest value is 25.0
2. Write a Java method to compute the average of three numbers.
Test Data:
Input the first number: 25
Input the second number: 45
Input the third number: 65
Expected Output:
The average value is 45.0
3. Write a Java method to display the middle character of a string.
Note: a) If the length of the string is odd there will be two middle characters.
b) If the length of the string is even there will be one middle character.
Test Data:
Input a string: 350
Expected Output:
The middle character in the string: 5
4. Write a Java method to count all vowels in a string.
Test Data:
Input the string: w3resource
Expected Output:
Number of Vowels in the string: 4
5. Write a Java method to count all words in a string.
Test Data:
Input the string: The quick brown fox jumps over the lazy dog.
Expected Output:
Number of words in the string: 9
6. Write a Java method to compute the sum of the digits in an integer.
Test Data:
Input an integer: 25
Expected Output:
The sum is 7
7. Write a Java method to display the first 50 pentagonal numbers.
Note: A pentagonal number is a figurate number that extends the concept of triangular and square numbers to the pentagon, but, unlike the first two, the patterns involved in the construction of pentagonal numbers are not rotationally symmetrical.
Expected Output:
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
9. Write a Java method to print characters between two characters (i.e. A to P ).
Note: Prints 20 characters per line
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:
Input a year: 2017
false
11. Write a Java method to check whether a string is a valid password.
Password rules:
A password must have at least ten characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Expected Output:
1. A password must have at least eight characters.
2. A password consists of only letters and digits.
3. A password must contain at least two digits
Input a password (You are agreeing to the above Terms and Conditions.): abcd1234
Password is valid: abcd1234
12. Write a Java method (takes a number n as input) to displays an n-by-n matrix.
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
13. Write Java methods to calculate the area of a triangle.
Expected Output:
Input Side-1: 10
Input Side-2: 15
Input Side-3: 20
The area of the triangle is 72.6184377413890
14. Write a Java method to create the area of a pentagon.
Expected Output:
Input the number of sides: 5
Input the side: 6
The area of the pentagon is 61.93718642120281
15. Write a Java method to display the current date and time.
Expected Output:
Current date and time: Wednesday January 25, 2017 7:47:43
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)
Java Number Exercises
Java Numbers Programming: Exercises, Practice, Solution
Last update on April 23 2020 08:21:27 (UTC/GMT +8 hours)
Java Number Exercises [29 exercises with solution]
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.
2. Write a Java program to classify Abundant, deficient and perfect number (integers) between 1 to 10,000.
In number theory, an abundant number is a number for which the sum of its proper divisors is greater than the number itself.
Example :
The first few abundant numbers are:
12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100, 102,…
The integer 12 is the first abundant number. Its proper divisors are 1, 2, 3, 4 and 6 for a total of 16.
Deficient number: In number theory, a deficient number is a number n for which the sum of divisors σ(n)<2n, or, equivalently, the sum of proper divisors (or aliquot sum) s(n)<n. The value 2n − σ(n) (or n − s(n)) is called the number's deficiency.
As an example, divisors of 21 are 1, 3 and 7, and their sum is 11. Because 11 is less than 21, the number 21 is deficient. Its deficiency is 2 × 21 − 32 = 10.
The first few deficient numbers are:
1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19, 21, 22, 23, 25, 26, 27, 29, 31, 32, 33, …….
Perfect number: In number system, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself) i.e. σ1(n) = 2n.
The first perfect number is 6. Its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128.
Expected Output :
Number Counting [(integers) between 1 to 10,000]:
Deficient number: 7508
Perfect number: 4
Abundant number: 2488
3. Write a Java program to generate random integers in a specific range.
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 :
0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634
7. Write a Java program to display first 10 lucus numbers.
The Lucas numbers or series are an integer sequence named after the mathematician François Édouard Anatole Lucas, who studied both that sequence and the closely related Fibonacci numbers. Lucas numbers and Fibonacci numbers form complementary instances of Lucas sequences.
The sequence of Lucas numbers is: 2, 1, 3, 4, 7, 11, 18, 29, ….
Expected Output :
First ten Lucas a numbers:
2
1
3
4
7
11
18
29
47
76
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,
List 10 Catalan numbers:-
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
First 10 Happy numbers:
1
7
10
13
19
23
28
31
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
Input a number : 353
353 is not a Harshad Number.
13. Write a Java program to check whether a number is a Pronic Number or Heteromecic Number or not.
A pronic number is a number which is the product of two consecutive integers, that is, a number of the form n(n + 1).
The first few pronic numbers are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 … etc.
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
Input a number : 3210
Duck number
16. Write a Java program to check two numbers are Amicable numbers or not.
Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number.
The first ten amicable pairs are: (220, 284), (1184, 1210), (2620, 2924), (5020, 5564), (6232, 6368), (10744, 10856), (12285, 14595), (17296, 18416), (63020, 76084), and (66928, 66992).
Expected Output
Input the first number: 220
Input the second number: 284
These numbers are amicable.
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
It is not a Circular Prime number.
18. Write a Java program to check a number is a cube or not.
In arithmetic and algebra, the cube of a number n is its third power: the result of the number multiplied by itself twice:
n3 = n × n × n.
Input Data:
Input a number: 8
Expected Output
Number is a cube.
19. Write a Java program to check a number is a cyclic or not.
A cyclic number is an integer in which cyclic permutations of the digits are successive multiples of the number. The most widely known are 142857:
142857 × 1 = 142857
142857 × 2 = 285714
142857 × 3 = 428571
142857 × 4 = 571428
142857 × 5 = 714285
142857 × 6 = 857142
Input Data:
Input a number: 142857
Expected Output
It is a cyclic number.
20. Write a Java program to display first 10 Fermat numbers.
In mathematics, a Fermat number is a positive integer of the form
Fermat Number where n is a nonnegative integer.
The first few Fermat numbers are:
3, 5, 17, 257, 65537, 4294967297, 18446744073709551617, …
Expected Output
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
22. Write a program to check if a number is Mersenne number or not.
In mathematics, a Mersenne number is a number that can be written in the form M(n) = 2n − 1 for some integer n.
The first four Mersenne primes are 3, 7, 31, and 127
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
24. Write a Java program to check if a number is palindrome or not.
In number system a palindromic number is a number that is the same when written forwards or backwards, i.e., of the form .
The first few palindromic numbers are therefore are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, …
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
28. Write a Program in Java to check whether a number is an Armstrong Number or not.
Armstrong (Michael F. Armstrong) number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers
Expected Output
Input an integer: 153
Is Armstrong number? true
29.Write a Program in Java to check whether a number is a Lucky Number or not.
Expected Output
Input an integer: 25
Is Lucky number? true
Java I/O Programming: Exercises, Practice, Solution
Last update on February 26 2020 08:08:15 (UTC/GMT +8 hours)
1. Write a Java program to get a list of all file/directory names from the given.
2. Write a Java program to get specific files by extensions from a specified folder.
3. Write a Java program to check if a file or directory specified by pathname exists or not.
4. Write a Java program to check if a file or directory has read and write permission.
5. Write a Java program to check if given pathname is a directory or a file.
6. Write a Java program to compare two files lexicographically.
According to Wikipedia:
In mathematics, the lexicographic or lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product) is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters. This generalization consists primarily in defining a total order over the sequences (often called words in computer science) of elements of a finite totally ordered set, often called alphabet.
7. Write a Java program to get last modified time of a file.
8. Write Java program to read input from java console.
9. Write a Java program to get file size in bytes, kb, mb.
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.
12. Write a Java program to read a plain text file.
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.
16. Write a Java program to append text to an existing file.
17. Write a Java program to read first 3 lines from a file.
18. Write a Java program to find the longest word in a text file.
Java Collection: Exercises, Practice, Solution
Last update on February 26 2020 08:08:15 (UTC/GMT +8 hours)
[An editor is available at the bottom of the page to write and execute the scripts.]
Java Collection: ArrayList Exercises [22 exercises with solution]
1. Write a Java program to create a new array list, add some colors (string) and print out the collection.
2. Write a Java program to iterate through all elements in a array list.
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.
5. Write a Java program to update specific array element by given element.
6. Write a Java program to remove the third element from a array list.
7. Write a Java program to search an element in a array list.
8. Write a Java program to sort a given array list.
9. Write a Java program to copy one array list into another.
10. Write a Java program to shuffle elements in a array list.
11. Write a Java program to reverse elements in a array list.
12. Write a Java program to extract a portion of a array list.
13. Write a Java program to compare two array lists.
14. Write a Java program of swap two elements in an array list.
15. Write a Java program to join two array lists.
16. Write a Java program to clone an array list to another array list.
17. Write a Java program to empty an array list.
18. Write a Java program to test an array list is empty or not.
19. Write a Java program to trim the capacity of an array list the current list size.
20. Write a Java program to increase the size of an array list.
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.
Java Collection: LinkedList Exercises [26 exercises with solution]
1. Write a Java program to append the specified element to the end of a linked list.
2. Write a Java program to iterate through all elements in a linked list.
3. Write a Java program to iterate through all elements in a linked list starting at the specified position.
4. Write a Java program to iterate a linked list in reverse order.
5. Write a Java program to insert the specified element at the specified position in the linked list.
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.
15. Write a Java program of swap two elements in an linked list.
16. Write a Java program to shuffle the elements in a linked list.
17. Write a Java program to join two linked lists.
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.
23. Write a Java program to convert a linked list to array list.
24. Write a Java program to compare two linked lists.
25. Write a Java program to test an linked list is empty or not.
26. Write a Java program to replace an element in a linked list.
Java Collection: HashSet Exercises [12 exercises with solution]
1. Write a Java program to append the specified element to the end of a hash set.
2. Write a Java program to iterate through all elements in a hash list.
3. Write a Java program to get the number of elements in a hash set.
4. Write a Java program to empty an hash set.
5. Write a Java program to test a hash set is empty or not.
6. Write a Java program to clone a hash set to another hash set.
7. Write a Java program to convert a hash set to an array.
8. Write a Java program to convert a hash set to a tree set.
9. Write a Java program to convert a hash set to a List/ArrayList.
10. Write a Java program to compare two hash set.
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.
Java Collection: TreeSet Exercises [16 exercises with solution]
1. Write a Java program to create a new tree set, add some colors (string) and print out the tree set.
2. Write a Java program to iterate through all elements in a tree 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.
7. Write a Java program to get the number of elements in a tree set.
8. Write a Java program to compare two tree sets.
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.
Java Collection: PriorityQueue Exercises [12 exercises with solution]
1. Write a Java program to create a new priority queue, add some colors (string) and print out the elements of the priority queue.
2. Write a Java program to iterate through all elements in priority queue.
3. Write a Java program to add all the elements of a priority queue to another priority queue.
4. Write a Java program to insert a given element into a priority queue.
5. Write a Java program to remove all the elements from a priority queue.
6. Write a Java program to count the number of elements in a priority queue.
7. Write a Java program to compare two priority queues.
8. Write a Java program to retrieve the first element of the priority queue.
9. Write a Java program to retrieve and remove the first element.
10. Write a Java program to convert a priority queue to an array containing all of the elements of the queue.
11. Write a Java program to convert a Priority Queue elements to a string representation.
12. Write a Java program to change priorityQueue to maximum priorityqueue.
Java Collection: HashMap Exercises [12 exercises with solution]
1. Write a Java program to associate the specified value with the specified key in a HashMap.
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.
4. Write a Java program to remove all of the mappings from a map.
5. Write a Java program to check whether a map contains key-value mappings (empty) or not.
6. Write a Java program to get a shallow copy of a HashMap instance.
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.
Java Collection: TreeMap Exercises [26 exercises with solution]
1. Write a Java program to associate the specified value with the specified key in a Tree Map.
2. Write a Java program to copy a Tree Map content to another Tree Map.
3. Write a Java program to search a key in a Tree Map.
4. Write a Java program to search a value in a 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.
7. Write a Java program to sort keys in Tree Map by using comparator.
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.
Java Math: Exercises, Practice, Solution
Last update on April 15 2020 08:16:13 (UTC/GMT +8 hours)
In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.
Write a Java program to print all primes smaller than or equal to any given number.
21. Write a Java program to find the number which has the maximum number of distinct prime factors in a given range.
22. Write a Java program to find next smallest palindrome.
23. A fast scheme for evaluating a polynomial such as:
-19+ 7x- 4x2 + 6x3
when
x=3
is to arrange the computation as follows:((((0)x+6)x+(-4))x+7)x+(-19)
Write a Java program to compute the result from the innermost brackets.
24. Write a Java program to calculate the Binomial Coefficient of two positive numbers.
25. Write a Java program to calculate e raise to the power x using sum of first n terms of Taylor Series.
26. Write a Java program to print all prime factors of a given number.
27. Write a Java program to check if a given number is Fibonacci number or not.
Java Sorting Algorithm: Exercises, Practice, Solution
Last update on February 26 2020 08:08:12 (UTC/GMT +8 hours)
According to Wikipedia "Shell sort or Shell's method, is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort). The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange."
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.
Stooge sort is a recursive sorting algorithm with a time complexity of O(nlog 3 / log 1.5 ) = O(n2.7095...). The running time of the algorithm is thus slower compared to efficient sorting algorithms, such as Merge sort, and is even slower than Bubble sort.
19. Write a Java program to sort an array of given integers using Bucket Sort Algorithm.
Bucket sort is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort, a generalization of pigeonhole sort, and is a cousin of radix sort in the most-to-least significant digit flavor.
Java Search: Exercises, Practice, Solution
Last update on February 26 2020 08:08:14 (UTC/GMT +8 hours)
[An editor is available at the bottom of the page to write and execute the scripts.]
1. Write a Java program to find a specified element in a given array of elements using Binary Search.
2. Write a Java program to find a specified element in a given array of elements using Linear Search.
3. Write a Java program to find a specified element in a given sorted array of elements using Jump Search.
From Wikipedia, in computer science, a jump search or block search refers to a search algorithm for ordered lists. It works by first checking all items Lkm, where ℜ ∈ ℵ and m is the block size, until an item is found that is larger than the search key. To find the exact position of the search key in the list a linear search is performed on the sublist L[(k-1)m, km].
4. Write a Java program to find a specified element in a given array of elements using Interpolation Search.
From Wikipedia, Interpolation search is an algorithm for searching for a key in an array that has been ordered by numerical values assigned to the keys (key values). It was first described by W. W. Peterson in 1957. Interpolation search resembles the method by which people search a telephone directory for a name (the key value by which the book's entries are ordered): in each step the algorithm calculates where in the remaining search space the sought item might be, based on the key values at the bounds of the search space and the value of the sought key, usually via a linear interpolation. The key value actually found at this estimated position is then compared to the key value being sought. If it is not equal, then depending on the comparison, the remaining search space is reduced to the part before or after the estimated position. This method will only work if calculations on the size of differences between key values are sensible.
5. Write a Java program to find a specified element in a given sorted array of elements using Exponential search.
From Wikipedia, in computer science, an exponential search (also called doubling search or galloping search or Struzik search) is an algorithm, created by Jon Bentley and Andrew Chi-Chih Yao in 1976, for searching sorted, unbounded/infinite lists. There are numerous ways to implement this with the most common being to determine a range that the search key resides in and performing a binary search within that range. This takes O(log i) where i is the position of the search key in the list, if the search key is in the list, or the position where the search key should be, if the search key is not in the list.
6. Write a Java program to find a specified element in a given array of elements using Ternary search.
From Wikipedia, a ternary search algorithm is a technique in computer science for finding the minimum or maximum of a unimodal function. A ternary search determines either that the minimum or maximum cannot be in the first third of the domain or that it cannot be in the last third of the domain, then repeats on the remaining two thirds. A ternary search is an example of a divide and conquer algorithm.
7. Given is a 2-dimensional integer array [0..m-1, 0..n-1], each row and column of which is in ascending order (see example)
Find the row, column position of a specified number in a given 2-dimensional array Write a Java program to find the row, column position of a specified number (row, column position) in a given 2-dimensional array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment