P3- Java Programs
Set 1
1. Write a program to swap two numbers without using temporary variable.
2. Write a program to find the factorial of a given number.
3. Write a program to check whether the accepted number is a palindrome or not.
4. Write a program to check whether two accepted numbers are equal or not without using
relational and arithmetic operators
5. Write a program to print the following patterns
(i) 1 (ii) 1
1 1 2 3
1 1 1 4 5 6
7 8
6. Write a program to check whether the accepted year is a leap year or not.
7. Write a program to print alternate prime number with in 100.
8. Write a program to print twin primes within 100.
9. Write a program to print twisted prime numbers with in 1000.
10. Write a program to print all 3 digit even numbers from 1 to 300 that has 2 in its ten’s place.
11. Write a program to accept a number and check whether it is
(a) Armstrong Number
A positive integer of n digits is called an Armstrong number of order n (order is
number of digits) if abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
Ex. 153, 1634
(b) Niven Number
A number is said to be Niven if it is divisible by the sum of its digits.
Ex. 126 1+2+6 = 9 126 is divisible by 9
(c) Special Number
A number is said to be special number when the sum of factorial of its digits is
. equal to the number itself.
Example- 145 is a Special Number as 1!+4!+5!=145.
(d) Perfect Number
A number is said to be perfect if the sum of the factors including 1 and excluding
. the number is the same as the number itself
Example: 6 sum of the Factors of 6 excluding 6 1 + 2 + 3 = 6
(e) Happy Number
Take a positive number and replace the number by the sum of the squares of its
digits. Repeat the process until the number equals 1 digit number. If the number
. ends with 1 then it is called Happy number.
Example: 31 32 + 12 = 10, 12 + 02 = 1
(f) Automorphic Number
A number is said to be Automorphic if the number is present in the last digit(s) of
. its square.
Example: 25 25 * 25 = 625 25 is present as last two digits of 625
(g) Neon Number
A number is said to be Neon number if the sum of the digits of the square of the
number is equal to the number itself
Example: 9 9 * 9 = 81 8+1 = 9
(h) Unique Number
A number is said to be unique if digits are not repeated in it.
Example: 1234
12. Write a program to print the Fibonacci series upto n terms.
13. Write a program to print Pascal Triangle upto n lines.
14. Write a program to convert Binary to Decimal and Decimal to Binary.
15. Write a program to reverse the bits of an integer and print its decimal equivalent.
16. Write a program to replace all 0’s with 5 and print the new number.
18. Write a program to print the following series.
1 2 1 3 2 5 3 7 5 11 8 13 13 17
19. Write a program to find the sum of the following series.
1+2 1+2+3 1+2+3+......+n
-------- + ---------- + . . . . . + --------------------
1*2 1*2*3 1*2*3*......*n
20. Write a program to validate the ISBN code.
Set 2
1. Write a java program with a method named display which shouldn’t be overridden and the
display() should have an integer variable i, where the value of the variable shouldn’t be changed.
Display i inside display().
2. Count all distinct elements in the given an unsorted array
3. Write a java program to find second highest element in an array with 5 elements
4. Write a java program to sort an array with 5 integers in an ascending order.
5. Write java program to Separate all 0's towards left side and all 1's towards right side using
array's.
6. Write a java program to find the transpose of a given matrix.
7. Write a program to reverse an array without using another array.
8. Write a program to search sub element in an array.
9. Write a java program to remove duplicates from an array.
10. How to find largest number less than a given number and without a given digit?
11. To find the missing number in a given array of 1 to n .
Set 3
1. Write a program to find the duplicate words and their number of occurrences.
2. Write a program to count the number of words in a given string.
3. Write a program to count the total number of occurrences of a given character in a string
without using any loop.
4. Write a program to reverse a string.
(i) Using StringBuffer (ii) Without using StringBuffer (iterative method)
(iii) recursive method
5. Write a program to count the number of occurrences of each character in a string.
6. Write a program to remove all whitespaces from a string.
7. Write a program to check whether two strings are anagram or not.
8. Write a program to reverse each word of a given string.
9. Write a program to find the percentage of uppercase letters, lowercase letters, digits and
special characters in a given string.
10. Write a program to swap two strings without using third or temporary variable.
11. Write a program to find all permutations of a string.
12. Write a program to find the first repeated and non-repeated character in a given string.
13. Write a program to append a given string to a text file.
14. Write a program to find the number of characters, words, lines in a given text file.
15. Write a program to find the most repeated word in a given text file.
16. Write a program to check if the accepted string is a palindrome or not without reversing the
string.
17. Write a program to check if the accepted string is a palindrome or not using both iteration
and recursion.
18. Write a program to prove that strings are immutable in Java?
19. Write a program to print repeated characters of String.
20. Write a program to remove duplicates from an array without using the Java Collection API.
21. Write a program to print repeated characters of String.
22. Write a program to check if two accepted strings are the rotation of each other. A string is
said to be a rotation of other string if they contain same characters and the sequence is rotated
across any character. Example “dabc” is a rotation of “abcd” but “dbac” is not.
23. Write a program to count the number of vowels and consonants in a String.
24. Write a program to check if a string is a valid shuffle of two strings. You are given 3 strings
first, second and third. Third string is said to be a shuffle of first and second if it can be formed
by interleaving the characters of first and second String in a way that maintains the left to right
ordering of characters from each string.
For example, given first=”abc”, second=”def” and third=”dabecf” is a valid shuffle since it
preserves the character ordering of the two strings.
25. Write a program to check if a String contains another String like indexOf().
Note: You need to write a function t search for the existence of a string(target) in another string
(source). The function takes two strings as the input and returns the index where the second
string is found. If the target string cannot be found, then return -1.
The behavior of the function is similar to indexOf() method from java.lang.String class.
26. Write a program to remove a given character from string.
27. Write a program to find the smallest substring in a given string containing all characters of
another string.
Example:
Input : “this is a test string”
String Pattern: “tist”
Smallest window is : “t stri”
28. Program to convert Romans to numerals.
29. Print message without using system.out.println().
30. Program to convert a character array to string.
31. Program to handle all types of exceptions in java.
Set 4:
1. Design a parking lot using object-oriented principles with the following assumptions
1) The parking lot has multiple levels. Each level has multiple rows of spots.
2) The parking lot can park motorcycles, cars, and buses.
3) The parking lot has motorcycle spots, compact spots, and large spots.
4) A motorcycle can park in any spot.
5) A car can park in either a single compact spot or a single large spot.
6) A bus can park in five large spots that are consecutive and within the same row. It
cannot park in small spots.
2. Imagine you have a call center with three levels of employees: fresher, technical lead (TL),
. product manager (PM). There can be multiple employees, but only one TL or PM. An ...
. .incoming telephone call must be allocated to a fresher who is free. If a fresher can’t handle the
. call, he or she must escalate the call to technical lead. If the TL is not free or not able to handle
. it, then the call should be escalated to PM.
3. Design a chess game using object oriented principles.
1. Program to implement thread using runnable interface.
2. Program to create multiple threads.
3. Program for producer consumer problem: Producer’s job is to generate data, put it into the
buffer and start again and consumer is consuming the data one piece at a time. Make sure that the
producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to
remove data from a empty buffer.
4. Program to set priorities of thread.
5. Program to display all running thread.
6. Program for Synchronization block.
7. Program to stop thread execution with ctrl+c.
8. Program to print Fibonacci & reverse series.
9. Online train ticket booking: Here many users trying to book available tickets at the same time
(eg. tatkal booking), here application needs to handle different threads (different users request to
same/if tickets sold out/ not available then rest of the users will get correct response as not
available to book.
Set 5
1. Write a program to traverse (or iterate) Array List?
2. Write a program to convert List to Array.
3. Write a program to traverse(or iterate) Hash Set?
4. Given an element write a program to check if element (value) exists in Array List?
5. Given an element write a program to check if element exists in Hash Set?
6. Write a program to add elements to the Hash Map given the key and value data type is
String?
7. Write a program to add elements to Hash Set?
8. Write a program to sort Array List in descending order?
9. Write a program to add element at particular index of Array List?
10. Write a program to remove element from specified index of Array List?
11. Write a program to reverse Array List in java?
12. How to serialize an Array List in java?
13. How to synchronize an Array List in java?