Java Programming Coding Questions
Practice most asked Java programming coding interview questions with solutions for freshers and experienced developers.
1. Find factorial of a number
EasyInput: 5 Output: 120
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long fact = 1;
for(int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println(fact);
sc.close();
}
}
2. Check whether a number is prime
EasyInput: 7 Output: Prime
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n <= 1) {
System.out.println("Not Prime");
sc.close();
return;
}
for(int i = 2; i * i <= n; i++) {
if(n % i == 0) {
System.out.println("Not Prime");
sc.close();
return;
}
}
System.out.println("Prime");
sc.close();
}
}
3. Print Fibonacci series
EasyInput: 7 Output: 0 1 1 2 3 5 8
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 0, b = 1;
for(int i = 1; i <= n; i++) {
System.out.print(a + " ");
int c = a + b;
a = b;
b = c;
}
sc.close();
}
}
4. Reverse a number
EasyInput: 12345 Output: 54321
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int rev = 0;
while(n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
System.out.println(rev);
sc.close();
}
}
5. Check whether a number is palindrome
EasyInput: 121 Output: Palindrome
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int temp = n;
int rev = 0;
while(n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
if(temp == rev) {
System.out.println("Palindrome");
} else {
System.out.println("Not Palindrome");
}
sc.close();
}
}
6. Find largest of three numbers
EasyInput: 10 25 15 Output: 25
import java.util.Scanner;
public class LargestOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int max = Math.max(a, Math.max(b, c));
System.out.println(max);
sc.close();
}
}
7. Check Armstrong number
MediumInput: 153 Output: Armstrong
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int temp = n;
int digits = String.valueOf(n).length();
int sum = 0;
while(temp > 0) {
int digit = temp % 10;
sum += Math.pow(digit, digits);
temp /= 10;
}
if(sum == n) {
System.out.println("Armstrong");
} else {
System.out.println("Not Armstrong");
}
sc.close();
}
}
8. Find sum of array elements
Easy
Input: 5
10 20 30 40 50
Output: 150
import java.util.Scanner;
public class ArraySum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int sum = 0;
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
sum += arr[i];
}
System.out.println(sum);
sc.close();
}
}
9. Linear search in array
Easy
Input: 5
10 20 30 40 50
30
Output: Found
import java.util.Scanner;
public class LinearSearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int key = sc.nextInt();
boolean found = false;
for(int i = 0; i < n; i++) {
if(arr[i] == key) {
System.out.println("Found");
found = true;
break;
}
}
if(!found) {
System.out.println("Not Found");
}
sc.close();
}
}
10. Count digits of a number
EasyInput: 12345 Output: 5
import java.util.Scanner;
public class CountDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
while(n > 0) {
count++;
n /= 10;
}
System.out.println(count);
sc.close();
}
}
11. Swap two numbers without using third variable
MediumInput: 10 20 Output: 20 10
import java.util.Scanner;
public class SwapNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println(a + " " + b);
sc.close();
}
}
12. Reverse a string
EasyInput: hello Output: olleh
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
String reversed = new StringBuilder(str).reverse().toString();
System.out.println(reversed);
sc.close();
}
}
13. Check if string is palindrome
EasyInput: madam Output: Palindrome
import java.util.Scanner;
public class StringPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
String reversed = new StringBuilder(str).reverse().toString();
if(str.equals(reversed)) {
System.out.println("Palindrome");
} else {
System.out.println("Not Palindrome");
}
sc.close();
}
}
14. Find largest element in array
Easy
Input: 5
10 45 23 67 34
Output: 67
import java.util.Scanner;
public class LargestInArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int max = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
System.out.println(max);
sc.close();
}
}
15. Count vowels in a string
EasyInput: programming Output: 3
import java.util.Scanner;
public class CountVowels {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next().toLowerCase();
int count = 0;
for(int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u') {
count++;
}
}
System.out.println(count);
sc.close();
}
}
16. Find longest palindromic substring
HardInput: babad Output: bab (or aba)
import java.util.Scanner;
public class LongestPalindrome {
public static String expandAroundCenter(String s, int left, int right) {
while(left >= 0 && right < s.length() &&
s.charAt(left) == s.charAt(right)) {
left--;
right++;
}
return s.substring(left + 1, right);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
if(s == null || s.length() < 1) {
System.out.println("");
sc.close();
return;
}
String longest = "";
for(int i = 0; i < s.length(); i++) {
String odd = expandAroundCenter(s, i, i);
String even = expandAroundCenter(s, i, i + 1);
if(odd.length() > longest.length()) longest = odd;
if(even.length() > longest.length()) longest = even;
}
System.out.println(longest);
sc.close();
}
}
17. Implement merge sort
Hard
Input: 5
64 34 25 12 22
Output: 12 22 25 34 64
import java.util.Scanner;
public class MergeSort {
public static void merge(int[] arr, int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int[] L = new int[n1];
int[] R = new int[n2];
for(int i = 0; i < n1; i++)
L[i] = arr[l + i];
for(int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while(i < n1 && j < n2) {
if(L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while(i < n1) {
arr[k] = L[i];
i++;
k++;
}
while(j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
public static void mergeSort(int[] arr, int l, int r) {
if(l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
mergeSort(arr, 0, n - 1);
for(int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
sc.close();
}
}
18. Find all permutations of a string
HardInput: ABC Output: ABC ACB BAC BCA CAB CBA
import java.util.Scanner;
public class StringPermutations {
public static void permute(String str, String answer) {
if(str.length() == 0) {
System.out.print(answer + " ");
return;
}
for(int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
String left = str.substring(0, i);
String right = str.substring(i + 1);
String rest = left + right;
permute(rest, answer + ch);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
permute(str, "");
sc.close();
}
}
19. Solve N-Queens problem
HardInput: 4 Output: . Q . . . . . Q Q . . . . . Q .
import java.util.Scanner;
public class NQueens {
public static boolean isSafe(int[][] board, int row, int col, int n) {
for(int i = 0; i < col; i++)
if(board[row][i] == 1) return false;
for(int i = row, j = col; i >= 0 && j >= 0; i--, j--)
if(board[i][j] == 1) return false;
for(int i = row, j = col; i < n && j >= 0; i++, j--)
if(board[i][j] == 1) return false;
return true;
}
public static boolean solveNQueens(int[][] board, int col, int n) {
if(col >= n) return true;
for(int i = 0; i < n; i++) {
if(isSafe(board, i, col, n)) {
board[i][col] = 1;
if(solveNQueens(board, col + 1, n))
return true;
board[i][col] = 0;
}
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] board = new int[n][n];
if(solveNQueens(board, 0, n)) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
System.out.print(board[i][j] == 1 ? "Q " : ". ");
}
System.out.println();
}
} else {
System.out.println("No solution exists");
}
sc.close();
}
}
20. Find longest common subsequence (LCS)
Hard
Input: AGGTAB
GXTXAYB
Output: 4 (GTAB)
import java.util.Scanner;
public class LongestCommonSubsequence {
public static int lcs(String s1, String s2, int m, int n) {
int[][] dp = new int[m + 1][n + 1];
for(int i = 0; i <= m; i++) {
for(int j = 0; j <= n; j++) {
if(i == 0 || j == 0) {
dp[i][j] = 0;
} else if(s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();
int result = lcs(s1, s2, s1.length(), s2.length());
System.out.println(result);
sc.close();
}
}