QaMantra: Top 5 Most Popular Java Programs Asked in Selenium Interview in 2018

## Pages

### Que : write a Program for Fibonacci Series in Java

Ans : The Fibonacci Series is a sequence of Integers and In which Next number is sum of Two Previous numbers .

There is Two Methods To Print fibonacci Series

• Fibonacci series without recursion
• Fibonacci series with recursion

### Program :

Type 1 : Fibonacci series program without recursion in Java

```public class FibonacciProgramWithoutRecursion{
public static void main(String[] args) {
int a1=0,a2=1,a3,i,count=5;
System.out.print(a1+" "+a2 );
// 0 and 1 dont have previous number
for(i=2;i<count;++i)
{
a3=a1+a2;
System.out.print(" "+a3);
a1=a2;
a2=a3;
}}}  ```

### Type 2 : Fibonacci series program with recursion in Java

```public class FibonacciProgramWithRecursion{
static int b1=0,b2=1,b3=0;
public static void FibonnacciNo(int count){
if(count>0){
b3 = b1 + b2;
b1 = b2;
b2 = b3;
System.out.print(" "+b3);
FibonnacciNo(count-1);
}
}
public static void main(String args[]){
int count=5;
System.out.print(b1+" "+b2);//printing 0 and 1
FibonnacciNo(count-2);//n-2 because 2 numbers are already printed
}
}  ```

### Que : Write a Program for Prime Number in Java ?

Ans   : Prime Number is a number who can divide by 1 or itslef and Prime Number is a number greater than 1 ,Prime No. Likes - 3,5,7,11,13,17,19 and so on , We Should know two importants point regarding prime no is :

Note1 - 0 and 1 is not a prime number

Note2 - Only 2 is Even Prime Number rest of All Even no can devide by 2 or other numbers

### Program :

// we are going to write a program to check that 7 is prime no or not

```public class PrimeNumber{
public static void main(String args[]){
int i,a=0,flag=0;
int b=3;//it is the number to be checked
a=b/2;

// Condition written for Exclude 0 and 1

if(b==0||b==1){
System.out.println(b+" 0 and 1 can not be prime number");
}else{
for(i=2;i<=a;i++){
if(b%i==0){
System.out.println(b+" is not prime number");
flag=1;
break;
}
}
if(flag==0)  { System.out.println(b+" is prime number"); }
}
}
}   ```

### Que : Write a Program to find a Number is Palindrome number or not in Java ?

Ans :  A Palindrome is a Number that remains same when its digit are reversed , In This Program we are going to check  '545' is Palindrome or not

### Program :

```public class PalindromeNumber{
public static void main(String args[]){
int a,sum=0,temp;
// no to check palindrome or not
int n=545;
temp=n;
while(n>0){
a=n%10;  //getting remainder
sum=(sum*10)+a;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}  ```

### Que : Write a Factorial program in Java ?

Ans   :  The result of multiplying a sequence of descending natural numbers down to 1 , In Math Factorial of n is denoted by n! , Like : 5!=5*4*3*2*1

we are going to write factorial program in Two way :

1. Factorial program using Loop iteration
2. Factorial program using recursion

### Program :

```public class FactorialProgram{
public static void main(String args[]){
int i,f=1;
int n=5;//It is the number to calculate factorial
for(i=1;i<=n;i++){
f=f*i;
}
System.out.println("Factorial of "+n+" is: "+f);
}
}  ```

### Type 2 :  Factorial program using recursion

```public class FactorialProgram{
public static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String args[]){
int i,f=1;
int n=4;//It is the number to calculate factorial
f = factorial(n);
System.out.println("Factorial of "+n+" is: "+f);
}
}  ```

### Que : Write a Program to Check Armstrong no in Java ?

Ans   : Armstrong number is a number which is equal to sum of cubes of its digit .
Like : 371 = (3*3*3) + (7*7*7) + (1*1*1) , Here we are going to write a program that 371 is  armstrong number or not .

```public class ArmstrongProgram{
public static void main(String[] args)  {
int a=0,b,temp;
int n=371;//It is the number to check armstrong
temp=n;
while(n>0)
{
b=n%10;
n=n/10;
a=a+(b*b*b);
}
if(temp==a)
System.out.println(n +" is aarmstrong number");
else
System.out.println(n +" is Not armstrong number");
}
}  ```