Java Interview Programs - Part 1

1) Fibonacci series
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class FibonacciExample {
 public static void main(String args[]) {
  
  int n1 = 0, n2 = 1, n3, i, count = 10;
  
  System.out.print(n1 + " " + n2);// printing 0 and 1

                // loop starts from 2 because 0 and 1 are already printed
  for (i = 2; i < count; ++i)
  {
   n3 = n1 + n2;
   System.out.print(" " + n3);
   n1 = n2;
   n2 = n3;
  }
 }
}
Output: 0 1 1 2 3 5 8 13 21 34

2) Prime Number
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class PrimeExample {
 public static void main(String args[]) {
  int i, mid = 0, flag = 0;
  int n = 3;// it is the number to be checked
  mid = n / 2;
  if (n == 0 || n == 1) {
   System.out.println(n + " is not prime number");
  } else {
   for (i = 2; i <= mid; i++) {
    if (n % i == 0) {
     System.out.println(n + " is not prime number");
     flag = 1;
     break;
    }
   }
   if (flag == 0) {
    System.out.println(n + " is prime number");
   }
  } // end of else
 }
}
Output: 3 is prime number

3) Palindrome Number
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class PalindromeExample{

 public static void main(String args[]) {
  int r, rev= 0, temp;
  int n = 454;// It is the number variable to be checked for palindrome

  temp = n;
  while (n > 0) {
   r = n % 10; // getting remainder
   rev = (rev * 10) + r;
   n = n / 10;
  }
  if (temp == rev)
   System.out.println("palindrome number ");
  else
   System.out.println("not palindrome");
 }
}
Output: palindrome number 

4) Factorial of Number
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Factorial {
 public static void main(String args[]) {
  int i, fact = 1;
  int number = 4;// It is the number to calculate factorial
  fact = factorial(number);
  System.out.println("Factorial of " + number + " is: " + fact);
 }

 static int factorial(int n) {
  if (n == 0)
   return 1;
  else
   return (n * factorial(n - 1));
 }
}
Output: Factorial of 4 is: 24

5) Armstrong Number
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class ArmstrongNumber{
 public static void main(String[] args) {
  int c = 0, a, temp;
  int n = 153;// It is the number to check armstrong
  temp = n;
  while (n > 0) {
   a = n % 10;
   n = n / 10;
   c = c + (a * a * a);
  }
  if (temp == c)
   System.out.println("armstrong number");
  else
   System.out.println("Not armstrong number");
 }
} 
Output: armstrong number

6) Sum of digit
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class SumOfDigits {

 static int sum = 0;

 public static int getNumberSum(int n) {
  if (n == 0) {
   return sum;
  } else {
   sum = sum + n % 10;
   getNumberSum(n / 10);
  }
  return sum;
 }

 public static void main(String[] args) {
  int number = 124;
  System.out.println("Sum of Digits = " + getNumberSum(number));
 }
}
Output: Sum of Digits = 7

7) Floyd Triangle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class FloydTrangle {
 public static void main(String args[]) {
  int n=5, num = 1, c, d;

  System.out.println("Floyd's triangle :-");

  for (c = 1; c <= n; c++) {
   for (d = 1; d <= c; d++) {
    System.out.print(num + " ");
    num++;
   }

   System.out.println();
  }
 }
}
Output:

2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

8) Reverse Floyd Triangle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class TestClass {

 public static void main(String args[]) {
  int i, j, n = 15;
  
  for (i = 5; i >= 1; i--) {
   for (j = i; j >= 1; j--) {
    System.out.print((n--) + " ");
   }
   System.out.println();
  }
 }
}
Output: 
15 14 13 12 11 
10 9 8 7 
6 5 4 
3 2 


9) Swap 2 numbers without using 3rd variable
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class SwapNumbers{

 public static void main(String args[]) {
  int a=5, b=10, c;
  System.out.println("Before swap a = " + a + " b = " + b);
  a=a+b;
  b=a-b;
  a=a-b;
  System.out.println("After swap a = " + a + " b = " + b);
 } 
}
Output:
Before swap a = 5 b = 10
After swap a = 10 b = 5

10) Print 1 to 10 without using loops.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class TestClass {

 public static void main(String args[]) {
  printNumbers(1);
 }

 public static void printNumbers(int num) {
  if (num <= 10) {

   System.out.println(num + " ");
   printNumbers(num + 1);
  }
 }
}
Output: 1 2 3 4 5 6 7 8 9 10 

11) Reverse a number.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class ReverseNumber {

 public static void main(String[] args) {

  int num = 1234, reversed = 0;

  while (num != 0) {
   int digit = num % 10;
   reversed = reversed * 10 + digit;
   num /= 10;
  }

  System.out.println("Reversed Number: " + reversed);
 }
}
Output: Reversed Number: 4321

12) Find smallest number without using if-else conditions.
1
2
3
4
5
6
7
8
public class FindSmallNumber{

 public static void main(String[] args) {
  int x = 10, y = 5;
  System.out.println("Minimum of " + x + " and " + y + " is -> " + ((x < y) ? x : y));

 }
}
Output:Minimum of 10 and 5 is -> 5


13) If numbers are multiple of 3 print "Foo", if numbers are multiple of 7 print "Bar" and if numbers are multiple of both 3 and 7 print "FooBar".
public class FooBar {

 public static void main(String[] args) {

  for (int i = 1; i <= 50; i++) {

   if (i % 3 == 0 && i % 7 == 0) {
    System.out.println(i + " foo-bar");
   } else if (i % 3 == 0) {
    System.out.println(i + " foo");
   } else if (i % 7 == 0) {
    System.out.println(i + " bar");
   }
  }
 }
}


Next -->

No comments:

Post a Comment