Interview Preparation - Java Program for beginners
1. Write a program to accept
two numbers from the user, calculate the sum and display the same.
Expected output:
Enter the num1 value:
10
Enter the num2 value:
20
Sum of 10 and 20 is 30
2. Write a program to accept
the weight of 3 persons, calculate the total weight, determine the average
weight and display these details.
Expected output:
Enter the weight of the first person:
55.5
Enter the weight of the second person:
45.4
Enter the weight of the third person:
65.6
The sum of weight of the 3 persons is 166.5 Kgs and the average
weight is 55.5 Kgs
3. Write a program to accept
the following details of an employee: empno, name and monthly salary; calculate
the yearly salary and display the result.
Expected output:
Enter the empno:
1001
Enter the employee name:
Ramana
Enter the monthly salary:
25000
Hi Ramana! Your employee id is 1001, monthly salary is Rs 25,000
and yearly salary is Rs 300,000.
4. Write a program to accept
two numbers from the user, swap their values and display the result.
Expected output:
Enter the first number num1:
100
Enter the second number num2:
200
Before swap, the values of num1=100 and num2=200
After swap, the values of num1=200 and num2=100
5. Write a program to accept
the principal amount, rate of interest, time and calculate the simple interest.
Expected output:
Enter the principal amount:
20000
Enter the rate of interest
1.5
Enter the time (years)
2
Simple interest is 600
(Help: Simple Interest
formula ((p*t*r)/100))
IF Statement
6. Write a program to accept a number, if it is negative then covert it
to a positive number.
Expected output:
Enter a number:
-10
The result is: 10
7. Write a program to accept
the billing amount, if it is > 6000 then give a discount of 10% and display
the net amount.
Expected output:
Enter the billing amount:
6500
Your net billing amount: 5850
Enter the billing amount:
5500
Your net billing amount: 5500
8. The Sports Club
registration form has the following details: name, mobile number and age. Per
the membership policy, the person should be at least 18 years old to become a member.
Write a program to accept the details mentioned above; if the age is >18 years
then display the entered details with a congratulatory message, else the
following message should be displayed “Sorry! You need to be at least 18 years
old to get membership.”
Expected output:
Enter the name:
Lakshman
Enter the mobile number:
989999999
Enter the age:
16
“Sorry! You need to be at
least 18 years old to get membership.”
Enter the name:
Lakshman
Enter the mobile number:
989999999
Enter the age:
30
“Congratulations Lakshman for
your successful registration.”
Hint: Use return statement
in if block after displaying the
“Sorry…” message.
If – Else Statement
9. Write a program to accept a number from the user and determine whether
it is even or odd.
Expected output:
Enter a number:
15
The entered number 15 is odd
Enter a number
10
The entered number 10 is even
10. Write a program to accept two
numbers from the user and determine bigger of the two.
Expected output:
Enter the first number num1:
20
Enter the second number num2:
45
The bigger of the two numbers entered (20 and 45) is: 45
11. Write a program to accept two
numbers num1 and num2; when one is subtracted from the other, the result should
always be a positive number.
Expected output:
Enter the first number num1:
35
Enter the second number num2:
45
The result (difference) is: 10
Enter the first number num1:
45
Enter the second number num2:
35
The result (difference) is: 15
12. In a shopping mall, privileged
customers (if they have a membership card) are being given a 10% discount on
the billed amount, and the others are being given a 3% discount. Write a
program to accept the billing amount and confirm the membership card from the customer;
calculate and display the net amount to be paid by the customer.
Expected output:
Enter the bill amount:
5000
Do you have a membership card?
Y
Thank you! Your total bill amount is Rs 5000, discount is Rs 500
and net amount payable is Rs 4500.
Enter the bill amount:
5000
Do you have a membership card?
N
Thank you! Your total bill amount is Rs 5000, discount is Rs 150
and net amount payable is Rs 4850.
IF – ELSE – IF Statement
13. Write a program to accept 3
numbers from the user and find the biggest of them.
Expected output:
Enter the 1st number num1:
45
Enter the 2nd number num2:
75
Enter the 3rd number num3:
45
The biggest of the 3 numbers entered is: 75
14. Write a program to accept
the marks scored in three subjects; determine the sum and average of the
entered marks. Grade is awarded based on following criteria.
If average is < 35 --
“C”; >35 and <60 -- “B”; Otherwise -- “A”
Expected output:
Enter the marks scored in 1st subject:
40
Enter the marks scored in 2nd subject:
60
Enter the marks scored in 3rd subject:
80
Total marks: 180
Average is: 60.0
Grade: “B”
FOR Loop
15. Write a program to generate
the first 'N' natural numbers. Accept the value of 'N' from the user.
Expected output:
Enter the number of natural numbers to be generated:
5
First 5 natural numbers are : 1 2 3 4 5
16. Write a program to accept a
number and determine whether it is a prime number or not.
Expected output:
Enter any number:
9
The entered number 9 is not a prime number
Enter any number:
7
The entered number 7 is a prime number
17. Write a program to generate
the first 'N' natural numbers and print them in descending order.
Expected output:
Enter the number of natural numbers to be generated:
5
The first 5 natural numbers in descending order are: 5 4 3 2 1
18. Write a program to accept the lower bound number and the upper
bound number from the user and print the prime numbers between them.
Expected output:
Enter the lower bound value:
5
Enter the upper bound value:
15
The prime numbers between 5 and 15 are: 5 7 11 13
19. Write a program to accept a
number and print the Fibonacci series up to the entered number.
Expected output:
Enter the upper bound number to generate the Fibonacci numbers:
8
Fibonacci series up to the entered number is: 0 1 1 2 3 5 8
20. Write a program to accept a
number from the user and print its multiplication table (upto “times 10”).
Expected output:
Enter the number to generate its multiplication table:
19
Multiplication table for 19 is :
19 * 1 = 19
19 * 2 = 38
…..............
…..............
19* 10 = 190
21. Write a program to accept a
number and find its factorial.
Expected output:
Enter any number:
5
The factorial of 5 is 120
(Hint: 5! = 5 * 4 * 3 * 2 *
1)
22. Write a program to accept a
number “n” from the user; then display the sum of the series
1+1/2+1/3+……….+1/n.
23. Write a program to accept a
number “n” from the user; then display the series 1,3,5,7,9,…,n and find the
sum of the numbers in this series.
24. Write a program to accept a
number “n” from the user; find the sum of the series 1/23+1/33+1/43……..+1/n3
Write a program to generate
following patterns.
25.
1
2 3
4 5 6 7
8 9 10 11
26.
1 2 3
4 5 6
7 8 9
27.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
28.
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
29.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
30.
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
31.
19 38 57
76 95 114
133 152 171
32.
A B C
D E F
G H I
33.
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 3 2 1
34.
19 0 0
0 19 0
0 0 19
35.
* * * * * * * * *
*
*
*
*
*
* * * * * * * * *
36.
@ @ @ @
@ @
@ @ @ @
@ @
@ @
37.
* * * * * * *
* *
* *
* *
* * * * * * * * * * * *
* *
* *
* *
* * * * * * *
38. Write a program to find the
biggest, smallest and sum of the elements in the given 3 X 3 matrix.
5 6 7
4 5 6
5 6 7
39.
1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2
2 3 4 5 1
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2
2 3 4 5 1
40.
1 2 3 4
5 6 7
8 9
10
5 6 7
8 9
10
While – loop
41. Write a program to accept a number from the
user and count the number of digits in the number.
Expected output:
Enter any number:
14567
The number of digits in the entered number is 5
42. Write a program to accept a
number from the user and find the sum of digits in the entered number.
Expected output:
Enter any number:
14567
The sum of digits of the entered number is 22
43. Write a program to accept a
number from the user and find the reverse of the entered number.
Expected output:
Enter any number:
45646
Reverse of the entered number is 64654
44. Write a program to accept a
number from the user and determine whether it is an Armstrong number or not.
(Example: 153 is an Armstrong
number 1^3 + 5 ^3 +3 ^3 =153)
45. Write a program to accept a
number from the user and calculate the sum of digits of the number; repeat the
operation till the sum gets to be a single digit number.
Expected output:
Enter any number:
9981
Single digit sum is: 9
(Hint: 9+9+8+1 = 27; 2+7 = 9)
46. Write a program to accept a
number from the user and count the number of prime digits.
Expected output:
Enter any number:
97512
Number of prime digits in the entered number is 3
47. Write a program to accept a
number and find the factorial of the number (using while loop).
48. Write a program to accept a
four digit number from the user and display its denomination.
Example: 5698
Output: 5*1000 =5000
6*100 =600
9*10 =90
8*1 =8
49. Write a program to accept a
five digit number from the user, increment each digit by one and display the
number (digit 9 gets incremented to 0).
Example:
Input: 14385
Output: 25496
50. Write a program to accept 2
numbers “m” and “n” from the user and determine m^n (without using predefined
functions).
Switch statement
51. Write a program to accept a
character from the user and check whether it is a vowel or consonant using switch statement.
52. Write a program to accept two
numbers num1, num2 and an operator. Simulate the calculator using switch statement.
Enter the 1st Operand num1:
10
Enter the 2nd Operand num2:
20
1. add 2. mul 3. div 4. mod 5. div
Enter the operator
1
The sum of 10 and 20 is 30
53. Write a program to generate
the following output
1 2
1 3
2 1
2 3
3 1
3 2
(Hint: Use continue
statement)
54. Write a program to add the first
7 terms of the following series using a for
loop:
1/1!+2/2!+3 /3!+....
55. Write a program to fill the
entire screen with a smiling face. The smiling face has an ASCII value 1.