Home » Freshers Interview

Category Archives: Freshers Interview

Pyramid Patterns

Write a program to print below pyarmid patterns ?

Program ::

Output

Prime number using Java

Write a Program to find if a number is prime or not ?

Solution :: Any number which is only divisible by 1 and the number itself is a prime number and it should not be divisible by any other number.

Program 1 ::
Algorithm ::
1. If num is 0,then it is not prime so return false
2. If num is 1 or 2 ,then it is prime so return true
3. In a loop try to divide the num with all the numbers less than the given input number ,if it is divisible by any
number then return false.
4. In rest cases return true.


output ::

Program 2

Algorithm ::
1. If num is 0,then it is not prime so return false
2. If num is 1 or 2 ,then it is prime so return true
3. In a loop try to divide the num with all the numbers less than the square root of given input number ,if it is divisible by any number then return false. Square root reduces the number of iteration.
4. In rest cases return true.


output ::

Fibonacci Series

Write a program to find Fibonacci Series of a function ?

let us define our funcion int fibonacci(int n) returns Fn.
If n = 0, then fibonacci() should return 0.
If n = 1, then it should return 1.
For n > 1, it should return Fn-1 + Fn-2

Let us do a dry run
n=0 –> 0
n=1 –> 1
n=2 –> 0+1=1
n=3 –> 1+1=2
n=4 –> 1+2=3
n=5 –> 2+3=5
n=6 –> 3+5=8
n=7 –> 5+8=13
n=8 –> 8+13=21
n=9 –> 13+21 =34
n=10 –> 21+32=55

A Fibonacci series looks like below
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ….

 
Iterative Solution ::

Output ::

 

Recursive Solution ::

Output::

Reverse a String

1. Write a Program to reverse a String using Java API ?
Solution :

OutPut : Reverse of Hello is olleH

2. Write a Program to reverse a String Iteratively?
Solution :

OutPut : Reverse of Hello is olleH

3. Write a Program to reverse a String recursively ?
Solution :

OutPut : Reverse of Hello is olleH