Code Snippets
Runnable Java code examples for testers: arrays, numbers and strings, with output and common mistakes.
Check Armstrong Number in Java: Full Logic + Code (2026)
Learn how to check Armstrong numbers in Java for 3-digit and n-digit numbers, print Armstrong numbers in a range, and avoid common mistakes, with FAQs.
Check Palindrome Number in Java: Full Code + Logic (2026)
Learn how to check if a number is a palindrome in Java using a while loop, recursion, and StringBuilder — with full code, traces, and interview FAQs.
Check Prime Number in Java: Optimized Code (sqrt(n) Method)
Learn how to check if a number is prime in Java using brute force and the optimized square-root method, plus how to print primes in a range with FAQs.
Check Leap Year in Java: Simple Logic + Full Code (2026)
Learn the exact leap year rule and how to check leap year in Java using if-else and ternary operators, including the century-year exception, with FAQs.
Find Power of a Number in Java: Loop, Math.pow() & Recursion
Learn how to calculate the power of a number in Java using a loop, Math.pow(), recursion, and fast exponentiation — with code, output, and FAQs.
Find Factorial of a Number in Java: Loop, Recursion & BigInteger
Learn how to find the factorial of a number in Java using a for loop, while loop, recursion, and BigInteger for large numbers — with code and FAQs.
Count Number of Digits in Java: 4 Methods (Loop, String, Log10)
Learn 4 ways to count the number of digits in an integer in Java — while loop, String.length(), Math.log10(), and recursion — with code and FAQs.
Sum of Digits in Java: While Loop, Recursion & Optimized Code
Learn how to find the sum of digits of a number in Java using a while loop and recursion, including how to reduce it to a single digit (digital root).
Reverse a Number in Java: 4 Methods (Loop, Recursion, StringBuilder)
Learn to reverse a number in Java using a while loop, recursion, and StringBuilder. Includes step-by-step traces, output, and common interview mistakes.
Find the Largest Among 3 Numbers in Java (4 Methods + Code)
Learn how to find the largest of three numbers in Java using if-else, nested if, Math.max, and ternary operators. Includes code, output, and FAQs.
Swap Two Numbers in Java: With and Without a Third Variable (2026 Guide)
Learn to swap two numbers in Java using a temp variable, arithmetic operators, and XOR — with code, output, memory explanation, and interview tips.
Check Positive, Negative, or Zero in Java (Code + Interview Answer)
Step-by-step Java program to check if a number is positive, negative, or zero. Includes if-else logic, ternary version, memory explanation, and FAQs.
How to Check Even or Odd Number in Java (5 Easy Methods + Code)
Learn how to check even or odd numbers in Java using modulus, bitwise, and ternary operators. Includes code, output, interview answers, and common mistakes.
Display Upper and Lower Triangular Matrix Elements in Java
Learn how to display the upper and lower triangular elements of a matrix in Java, with correct index conditions and full code examples.
Check if a Matrix Is an Identity Matrix in Java (Full Guide)
Learn how to check whether a matrix is an identity matrix in Java, verifying both the diagonal and off-diagonal elements correctly.
Multiply Two Matrices in Java (Full Guide With Dimension Rules)
Learn how to multiply two matrices in Java using triple nested loops, understand dimension compatibility rules, and avoid common mistakes
Find the Sum of Diagonal Elements of a Matrix in Java (Both Diagonals)
Learn how to find the sum of both diagonals in a Java matrix, avoiding the classic double-counting bug for odd-sized matrices.
Print Diagonal Elements of a Matrix in Java (Both Diagonals)
Learn how to print the main and anti-diagonal elements of a matrix in Java, with the index formulas explained clearly for each.
Find the Transpose of a Matrix in Java (In-Place & New Matrix Methods)
Learn how to find the transpose of a matrix in Java for both square and non-square matrices, including the in-place swap technique.
Find Sum of Each Column in 2D Array in Java
Learn how to calculate the sum of each column in a Java 2D array using nested loops with swapped iteration order, and streams.