Practice 100 Java Basic Part II Programming Questions
Q1. Write a Java program to calculate the square of a given number.
Input:
Enter a number: 7
Expected Output:
Square = 49
Q2. Write a Java program to swap two numbers using a temporary variable.
Input:
Enter first number: 10
Enter second number: 20
Expected Output:
After swapping:
First number = 20
Second number = 10
Q3. Write a Java program to calculate the area of a triangle using its base and height.
Input:
Enter base: 5
Enter height: 10
Expected Output:
Area of the triangle = 25.0
Q4. Write a Java program to check if a given year is a leap year.
Input:
Enter a year: 2024
Expected Output:
2024 is a leap year.
Q5. Write a Java program to reverse a number.
Input:
Enter a number: 12345
Expected Output:
Reversed number = 54321
Q6. Write a Java program to calculate the sum of digits of a given number.
Input:
Enter a number: 456
Expected Output:
Sum of digits = 15
Q7. Write a Java program to display all even numbers up to a given number.
Input:
Enter the limit: 10
Expected Output:
Even numbers: 2 4 6 8 10
Q8. Write a Java program to check if a number is positive, negative, or zero.
Input:
Enter a number: -5
Expected Output:
The number is negative.
Q9. Write a Java program to find the largest of three numbers.
Input:
Enter three numbers: 12 45 30
Expected Output:
The largest number is 45.
Q10. Write a Java program to calculate the perimeter of a rectangle.
Input:
Enter length: 8
Enter width: 5
Expected Output:
Perimeter of the rectangle = 26
Q11. Write a Java program to find the ASCII value of a character.
Input:
Enter a character: A
Expected Output:
ASCII value of A = 65
Q12. Write a Java program to count the number of vowels in a string.
Input:
Enter a string: Hello World
Expected Output:
Number of vowels = 3
Q13. Write a Java program to print a multiplication table for a given number.
Input:
Enter a number: 5
Expected Output:
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Q14. Write a Java program to find the sum of an array of integers.
Input:
Enter array elements: 1 2 3 4 5
Expected Output:
Sum = 15
Q15. Write a Java program to reverse a string.
Input:
Enter a string: Java
Expected Output:
Reversed string = avaJ
Q16. Write a Java program to check if a character is a vowel or a consonant.
Input:
Enter a character: e
Expected Output:
e is a vowel.
Q17. Write a Java program to find the second largest number in an array.
Input:
Enter array elements: 10 20 30 40 50
Expected Output:
Second largest number = 40
Q18. Write a Java program to calculate the simple interest.
Input:
Enter principal: 1000
Enter rate: 5
Enter time: 2
Expected Output:
Simple interest = 100
Q19. Write a Java program to check if a string contains only digits.
Input:
Enter a string: 12345
Expected Output:
The string contains only digits.
Q20. Write a Java program to find the greatest common divisor (GCD) of two numbers.
Input:
Enter two numbers: 12 15
Expected Output:
GCD = 3
Q21. Write a Java program to convert a binary number to a decimal number.
Input:
Enter a binary number: 1010
Expected Output:
Decimal equivalent = 10
Q22. Write a Java program to convert a decimal number to a binary number.
Input:
Enter a decimal number: 15
Expected Output:
Binary equivalent = 1111
Q23. Write a Java program to count the number of words in a string.
Input:
Enter a string: Java is fun
Expected Output:
Number of words = 3
Q24. Write a Java program to find the smallest element in an array.
Input:
Enter array elements: 10 20 5 30
Expected Output:
Smallest element = 5
Q25. Write a Java program to calculate the average of an array of integers.
Input:
Enter array elements: 2 4 6 8 10
Expected Output:
Average = 6.0
Q26. Write a Java program to check if a number is divisible by both 3 and 5.
Input:
Enter a number: 15
Expected Output:
15 is divisible by both 3 and 5.
Q27. Write a Java program to calculate the power of a number using a loop.
Input:
Enter base: 2
Enter exponent: 3
Expected Output:
Result = 8
Q28. Write a Java program to calculate the compound interest.
Input:
Enter principal: 1000
Enter rate: 5
Enter time: 2
Enter number of times interest applied per time: 2
Expected Output:
Compound interest = 1102.5
Q29. Write a Java program to check if an array is sorted in ascending order.
Input:
Enter array elements: 1 2 3 4 5
Expected Output:
The array is sorted in ascending order.
Q30. Write a Java program to calculate the sum of even and odd numbers separately in a given range.
Input:
Enter start: 1
Enter end: 10
Expected Output:
Sum of even numbers = 30
Sum of odd numbers = 25
Q31. Write a Java program to find the factorial of a number using a loop.
Input:
Enter a number: 5
Expected Output:
Factorial = 120
Q32. Write a Java program to print the Fibonacci series up to a given number of terms.
Input:
Enter number of terms: 7
Expected Output:
Fibonacci series: 0 1 1 2 3 5 8
Q33. Write a Java program to check if a given string is a palindrome.
Input:
Enter a string: madam
Expected Output:
The string is a palindrome.
Q34. Write a Java program to calculate the product of digits of a given number.
Input:
Enter a number: 123
Expected Output:
Product of digits = 6
Q35. Write a Java program to check if a number is a perfect square.
Input:
Enter a number: 16
Expected Output:
16 is a perfect square.
Q36. Write a Java program to count the number of occurrences of a character in a string.
Input:
Enter a string: programming
Enter a character: g
Expected Output:
The character 'g' appears 2 times.
Q37. Write a Java program to check if two strings are anagrams of each other.
Input:
Enter first string: listen
Enter second string: silent
Expected Output:
The strings are anagrams.
Q38. Write a Java program to remove all spaces from a string.
Input:
Enter a string: Hello World
Expected Output:
String without spaces: HelloWorld
Q39. Write a Java program to calculate the sum of squares of the first N natural numbers.
Input:
Enter a number: 3
Expected Output:
Sum of squares = 14
Q40. Write a Java program to generate the first N prime numbers.
Input:
Enter N: 5
Expected Output:
First 5 prime numbers: 2 3 5 7 11
Q41. Write a Java program to check if a number is an Armstrong number.
Input:
Enter a number: 153
Expected Output:
153 is an Armstrong number.
Q42. Write a Java program to check if a number is a palindrome.
Input:
Enter a number: 121
Expected Output:
The number is a palindrome.
Q43. Write a Java program to calculate the LCM of two numbers.
Input:
Enter two numbers: 12 15
Expected Output:
LCM = 60
Q44. Write a Java program to sort an array of strings in alphabetical order.
Input:
Enter strings: apple banana cherry
Expected Output:
Sorted strings: apple banana cherry
Q45. Write a Java program to find the length of the longest word in a string.
Input:
Enter a string: Java programming language
Expected Output:
The longest word is 'programming' with length 11.
Q46. Write a Java program to find the sum of diagonal elements of a 2D matrix.
Input:
Enter 2D matrix:
1 2 3
4 5 6
7 8 9
Expected Output:
Sum of diagonal elements = 15
Q47. Write a Java program to print the transpose of a 2D matrix.
Input:
Enter 2D matrix:
1 2
3 4
5 6
Expected Output:
Transpose:
1 3 5
2 4 6
Q48. Write a Java program to find the mode of an array of integers.
Input:
Enter array elements: 2 3 2 4 5 2
Expected Output:
Mode = 2
Q49. Write a Java program to find the frequency of elements in an array.
Input:
Enter array elements: 1 2 2 3 3 3
Expected Output:
Element: 1 Frequency: 1
Element: 2 Frequency: 2
Element: 3 Frequency: 3
Q50. Write a Java program to find all prime factors of a number.
Input:
Enter a number: 28
Expected Output:
Prime factors: 2 2 7
Q51. Write a Java program to find the sum of odd numbers in a given range.
Input:
Enter start: 1
Enter end: 10
Expected Output:
Sum of odd numbers = 25
Q52. Write a Java program to calculate the GCD of two numbers using recursion.
Input:
Enter two numbers: 48 18
Expected Output:
GCD = 6
Q53. Write a Java program to check if a number is a power of 2.
Input:
Enter a number: 16
Expected Output:
16 is a power of 2.
Q54. Write a Java program to print the prime numbers up to N.
Input:
Enter N: 10
Expected Output:
Prime numbers up to 10: 2 3 5 7
Q55. Write a Java program to reverse an integer.
Input:
Enter a number: 12345
Expected Output:
Reversed number = 54321
Q56. Write a Java program to find the square root of a number.
Input:
Enter a number: 25
Expected Output:
Square root = 5.0
Q57. Write a Java program to print the pattern of stars in a triangle shape.
Input:
Enter number of rows: 5
Expected Output:
*
**
***
****
*****
Q58. Write a Java program to find the frequency of vowels in a string.
Input:
Enter a string: Hello World
Expected Output:
Vowels frequency: 3
Q59. Write a Java program to find the area of a rectangle.
Input:
Enter length: 10
Enter width: 5
Expected Output:
Area = 50
Q60. Write a Java program to convert temperature from Celsius to Fahrenheit.
Input:
Enter temperature in Celsius: 25
Expected Output:
Temperature in Fahrenheit = 77.0
Q61. Write a Java program to print the Fibonacci series up to N terms using recursion.
Input:
Enter number of terms: 6
Expected Output:
Fibonacci series: 0 1 1 2 3 5
Q62. Write a Java program to find the sum of digits of a number.
Input:
Enter a number: 123
Expected Output:
Sum of digits = 6
Q63. Write a Java program to generate the Pascal’s Triangle.
Input:
Enter number of rows: 5
Expected Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Q64. Write a Java program to swap two numbers using a temporary variable.
Input:
Enter first number: 5
Enter second number: 10
Expected Output:
After swapping: first number = 10, second number = 5
Q65. Write a Java program to find the largest element in an array.
Input:
Enter array elements: 5 10 15 20
Expected Output:
Largest element = 20
Q66. Write a Java program to calculate the sum of cubes of first N natural numbers.
Input:
Enter N: 3
Expected Output:
Sum of cubes = 36
Q67. Write a Java program to find the sum of even numbers in a given range.
Input:
Enter start: 1
Enter end: 10
Expected Output:
Sum of even numbers = 30
Q68. Write a Java program to remove duplicate elements from an array.
Input:
Enter array elements: 1 2 2 3 4 4 5
Expected Output:
Array without duplicates: 1 2 3 4 5
Q69. Write a Java program to check if a number is prime.
Input:
Enter a number: 13
Expected Output:
13 is a prime number.
Q70. Write a Java program to print all the prime numbers up to N using a for loop.
Input:
Enter N: 20
Expected Output:
Prime numbers up to 20: 2 3 5 7 11 13 17 19
Q71. Write a Java program to find the reverse of a string without using any library functions.
Input:
Enter a string: Java
Expected Output:
Reversed string = avaJ
Q72. Write a Java program to check if a string is a valid number (integer or decimal).
Input:
Enter a string: 12.34
Expected Output:
The string is a valid number.
Q73. Write a Java program to calculate the perimeter of a rectangle.
Input:
Enter length: 10
Enter width: 5
Expected Output:
Perimeter = 30
Q74. Write a Java program to find the sum of elements in the main diagonal of a matrix.
Input:
Enter matrix elements:
1 2 3
4 5 6
7 8 9
Expected Output:
Sum of diagonal elements = 15
Q75. Write a Java program to check if a number is divisible by 7 but not 5.
Input:
Enter a number: 14
Expected Output:
14 is divisible by 7 but not by 5.
Q76. Write a Java program to count the number of even and odd digits in a number.
Input:
Enter a number: 12345
Expected Output:
Even digits = 2, Odd digits = 3
Q77. Write a Java program to check if a number is a perfect number.
Input:
Enter a number: 28
Expected Output:
28 is a perfect number.
Q78. Write a Java program to print the sum of two 2D matrices.
Input:
Enter first matrix:
1 2 3
4 5 6
Enter second matrix:
7 8 9
10 11 12
Expected Output:
Sum of matrices:
8 10 12
14 16 18
Q79. Write a Java program to calculate the area of a circle.
Input:
Enter radius: 7
Expected Output:
Area = 153.94
Q80. Write a Java program to find the second smallest number in an array.
Input:
Enter array elements: 10 20 30 5 15
Expected Output:
Second smallest number = 10
Q81. Write a Java program to check if a number is an abundant number.
Input:
Enter a number: 12
Expected Output:
12 is an abundant number.
Q82. Write a Java program to find the sum of squares of the even numbers in a given range.
Input:
Enter start: 1
Enter end: 10
Expected Output:
Sum of squares of even numbers = 220
Q83. Write a Java program to check if a number is a strong number.
Input:
Enter a number: 145
Expected Output:
145 is a strong number.
Q84. Write a Java program to remove the first occurrence of a character in a string.
Input:
Enter a string: programming
Enter character to remove: r
Expected Output:
String after removal: pogramming
Q85. Write a Java program to find the number of prime numbers in a given range.
Input:
Enter start: 1
Enter end: 10
Expected Output:
Number of primes = 4
Q86. Write a Java program to check if a number is an odd number.
Input:
Enter a number: 7
Expected Output:
7 is an odd number.
Q87. Write a Java program to print the multiplication table of a given number.
Input:
Enter a number: 5
Expected Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
Q88. Write a Java program to calculate the square of a number using a function.
Input:
Enter a number: 4
Expected Output:
Square = 16
Q89. Write a Java program to swap two numbers without using a temporary variable.
Input:
Enter first number: 5
Enter second number: 10
Expected Output:
After swapping: first number = 10, second number = 5
Q90. Write a Java program to reverse a number using recursion.
Input:
Enter a number:
12345
Expected Output:
Reversed number = 54321
Q91. Write a Java program to print the first N Fibonacci numbers using a loop.
Input:
Enter N: 6
Expected Output:
Fibonacci numbers: 0 1 1 2 3 5
Q92. Write a Java program to calculate the factorial of a number using recursion.
Input:
Enter a number: 5
Expected Output:
Factorial = 120
Q93. Write a Java program to check if a given number is an Armstrong number.
Input:
Enter a number: 153
Expected Output:
153 is an Armstrong number.
Q94. Write a Java program to print the sum of the digits of a number.
Input:
Enter a number: 9876
Expected Output:
Sum of digits = 30
Q95. Write a Java program to reverse a string using a for loop.
Input:
Enter a string: Java
Expected Output:
Reversed string = avaJ
Q96. Write a Java program to find the sum of elements in an array.
Input:
Enter array elements: 1 2 3 4 5
Expected Output:
Sum of elements = 15
Q97. Write a Java program to print all the numbers divisible by 5 in a given range.
Input:
Enter start: 1
Enter end: 20
Expected Output:
Numbers divisible by 5: 5 10 15
Q98. Write a Java program to find the sum of squares of odd numbers in a given range.
Input:
Enter start: 1
Enter end: 10
Expected Output:
Sum of squares of odd numbers = 165
Q99. Write a Java program to check if a number is divisible by both 3 and 5.
Input:
Enter a number: 15
Expected Output:
15 is divisible by both 3 and 5.
Q100. Write a Java program to calculate the area of a triangle given its base and height.
Input:
Enter base: 10
Enter height: 5
Expected Output:
Area = 25.0
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!