Java Program For Practice
Java Program For Practice
1. Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.
2. Write a program to enter the numbers till the user wants and at the end
the program should display the largest and smallest numbers entered.
3. Write a program to print Fibonacci series of n terms where n is input by
user:
0 1 1 2 3 5 8 13 24 .....
4. Write a program that generates a random number and asks the user to
guess what the number is. If the user's guess is higher than the random
number, the program should display "Too high, try again." If the user's
guess is lower than the random number, the program should display "Too
low, try again." The program should use a loop that repeats until the user
correctly guesses the random number.
5. Write a program to print following:
iv) * v) 1 vi) 1
222 212
*** 33333 32123
***** 4444444 4321234
******* 555555555 543212345
*********
Java Method Exercises
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
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:
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:
Test Data:
Input the string: w3resource
Expected Output:
Number of Vowels in the string: 4
8. 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
9. 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
10. 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