[go: up one dir, main page]

0% found this document useful (0 votes)
17 views4 pages

Java Interview Programs

Uploaded by

keerthiv2340
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Java Interview Programs

Uploaded by

keerthiv2340
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

15 Most Common Java Interview Programs

1. Reverse a String
public class ReverseString {
public static String reverse(String input) {
return new StringBuilder(input).reverse().toString();
}
}

2. Check Palindrome
public class PalindromeCheck {
public static boolean isPalindrome(String str) {
return str.equals(new StringBuilder(str).reverse().toString());
}
}

3. Factorial of a Number
public class Factorial {
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
}

4. Fibonacci Series
public class Fibonacci {
public static void printFibonacci(int count) {
int a = 0, b = 1;
for (int i = 0; i < count; i++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
}
}

5. Prime Number Check


public class PrimeCheck {
public static boolean isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
}

6. Swap Two Numbers without Temp


public class SwapNumbers {
public static void swap(int a, int b) {
System.out.println("Before: a=" + a + ", b=" + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("After: a=" + a + ", b=" + b);
}
}

7. Count Digits
public class CountDigits {
public static int countDigits(int n) {
int count = 0;
while (n != 0) {
n /= 10;
count++;
}
return count;
}
}

8. Reverse a Number
public class ReverseNumber {
public static int reverse(int num) {
int rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
return rev;
}
}

9. Sum of Digits
public class SumOfDigits {
public static int sumDigits(int num) {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
}

10. Check Armstrong Number


public class Armstrong {
public static boolean isArmstrong(int num) {
int temp = num, sum = 0, digits = 0;
while (temp != 0) {
digits++;
temp /= 10;
}
temp = num;
while (temp != 0) {
sum += Math.pow(temp % 10, digits);
temp /= 10;
}
return sum == num;
}
}

11. Find Largest Element in Array


public class LargestInArray {
public static int findMax(int[] arr) {
int max = arr[0];
for (int i : arr) {
if (i > max) max = i;
}
return max;
}
}

12. Linear Search


public class LinearSearch {
public static int search(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) return i;
}
return -1;
}
}

13. Binary Search


public class BinarySearch {
public static int search(int[] arr, int key) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) return mid;
if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}
}

14. Check Even or Odd


public class EvenOdd {
public static boolean isEven(int n) {
return n % 2 == 0;
}
}

15. Print Multiplication Table


public class MultiplicationTable {
public static void printTable(int n) {
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
}
}

You might also like