Вы находитесь на странице: 1из 24

https://www.w3resource.com/java-exercises/basic/index.

php
Java Number Exercises

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 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 prime number that can be written in the form Mn = 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 check if a given number is circular prime or not.
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
Expected Output
Input a number: 1193
Output:
1193
1931
9311
3119

1193 is a circular_num Prime number.

28. 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
https://www.w3resource.com/java-exercises/numbers/index.php

Java Array Exercises


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 :
12
34
Output:
[
[1],
[2, 3],
[4]
]
38. Write a Java program to get the majority element from an given array of integers containing
duplicates.
Majority element: A majority element is an element that appears more than n/2 times where n is
the size of the array.
39. Write a Java program to print all the LEADERS in the array.
Note: An element is leader if it is greater than all the elements to its right side.
40. Write a Java program to find the two elements from a given array of positive and negative
numbers such that their sum is closest to zero.
41. Write a Java program to find smallest and second smallest elements of a given array.
42. Write a Java program to segregate all 0s on left side and all 1s on right side of a given array of
0s and 1s.
43. Write a Java program to find all combination of four elements of an given array whose sum is
equal to a given value.
44. Write a Java program to count the number of possible triangles from an given unsorted array
of positive integers.
45. Write a Java program to cyclically rotate a given array clockwise by one.
46. Write a Java program to check whether there is a pair with a specified sum of a given sorted
and rotated array.
47. Write a Java program to find the rotation count in a given rotated sorted array of integers.
48. Write a Java program to arrange the elements of an given array of integers where all negative
integers appear before all the positive integers.
49. Write a Java program to arrange the elements of an given array of integers where all positive
integers appear before all the negative integers.
50. Write a Java program to sort an array of positive integers of an given array, in the sorted array
the value of the first element should be maximum, second value should be minimum value, third
should be second maximum, fourth second be second minimum and so on.
51. Write a Java program to separate 0s on left side and 1s on right side of an array of 0s and 1s in
random order.
52. Write a Java program to separate even and odd numbers of an given array of integers. Put all
even numbers first, and then odd numbers.
53. Write a Java program to replace every element with the next greatest element (from right
side) in an given array of integers.
Java String Exercises
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 bc de f ghi j
=========================
36 10 7 40 2 16 42 1 6 20

k l m n o pq 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 bc d e f g hi j
===========================
36 10 7 40 33 16 42 32 6 20

k l m n o pq 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
12   11   10   9  8    7   6   5   4  3   2   1
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: kitandkatcaketoket
The new string is: ktandktcaktokt
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: bigbatfatman
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 return the number of index positions from two given strings where
they contain the same substring of two characters.
Sample Output:
The given strings are: aabxyyz and aabuyyz
The sum of the digits in the string is: 4
99. 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
100. 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

Вам также может понравиться