Skip to main content

Interview program in java

program to reverse a string in java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package logic;
 
import java.util.Scanner;
 
public final class Reversestring 
{
	public static void main(String[] args)
	{
 
	 String input;
	 String reverse="";
 
		Scanner scn=new Scanner(System.in);
	    System.out.println("enter a string");
 
	    input=scn.nextLine();
 
        int length=input.length();
	    for(int i=length-1;i>=0;i--)
	    {
	    	reverse=reverse +input.charAt(i);
	    }
 
	    System.out.println("reverse string  "+reverse);
	}
 
}
 
output:
enter a string
bhavesh
reverse string  hsevahb

Java program to generate fibonacci series upto N number

Fibonacci series:0 1 1 2 3 5 8 13 21 34 55

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package logic;
import java.util.Scanner;
 
public class Fibonacci 
{
	public static void main(String[] args)
	{
	 Scanner c=new Scanner(System.in);
 
	 System.out.print("enter a number");
 
     int n=c.nextInt();
     int n1=0;
     int n2=1;
     int n3=0;
     System.out.print(0+" "+1);
     while(n3<n)
     {
    	 n3=n1+n2;
    	 n1=n2;
    	 n2=n3;
    	 System.out.print(n3+" ");
 
     }
 
	}
 
}
 
output:
enter a number
50
0 1 1  2  3  5  8  13  21  34  55

Write a Java program to find if a number is prime number or not
Prime number: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29….

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package logic;
 
import java.util.Scanner;
 
public class Primenumber {
 
 
	public static void main(String[] args) 
	{
 
		System.out.println("enter a number");
		Scanner c=new Scanner(System.in);
		int a=c.nextInt();
		int d=0;
 
    for(int i=2;i<a;i++)
    {
 
	  if(a%i==0)
	    {
	     	d++;
	    }
 
    }
 
     if(d==0)
     {
	   System.out.println("It is a prime Number");
     } 
     else
     {
	    System.out.println("It is not a prime Number");
 
     }
  }	
}
 
output:
enter a number
17
It is a prime Number

Write a java program find factorial value of given number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package logic;
 
import java.util.Scanner;
 
public class Factorial 
{
 
	public static void main(String[] args) {
 
		int f;
		Scanner c=new Scanner(System.in);
		System.out.println("enter a number");
		f=c.nextInt();
		int x=1;
 
 
		for(int i=1;i<=f;i++)
 
		{
		x=x*i	;
 
		}
 
		System.out.println("factorial value "+x);
 
	// using while loop	
 
	/*	while(f!=0)
 
	{
 
			 x=x*f;
			 f--;
		}
		System.out.println("factoral value ="+x);
 
		*/
 
 
	}
 
}
 
output:
enter a number
5
factorial value 120

Write a java program to check whether the given year is leap year or not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package logic;
 
import java.util.Scanner;
 
public class Leapyear 
{
	public static void main(String[] args)
	{
		int year;
 
		Scanner c=new Scanner(System.in);
		System.out.println("enter a year");
		year=c.nextInt();
 
		if(year%4==0 && year%400==0)
		{
			System.out.println("it is leap year");
		}
		else if(year%100==0)
		{
			 System.out.println("not a leap year");
		}
 
		else 
		{
			 System.out.println("it is not a leap year");
		}
 
 
 
	}
 
}
 
output:
enter a year
2016
it is not a leap year

Write a Java program to check if a number is Armstrong or not ?
Armstrong number:0,1,153,370,371,407

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package logic;
 
import java.util.Scanner;
 
public class Armstrong 
{
 
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Scanner c=new Scanner(System.in);
 
		System.out.println("enter a number");
        int a=c.nextInt();
 
        int temp=a;
 
       int  sum=0;
 
 
        while(a!=0)
        {
 
        	int d=a%10;
        	sum=sum+d*d*d;
        	a=a/10;
 
        }
        if(sum==temp)
        {
        	System.out.println("it is armstrong number");
        }
 
        else
        {
        	System.out.println("not armstrong");
        }
	}
 
}
 
output:
enter a number
153
it is armstrong number

How to Swap two numbers without using third variable in Java?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package logic;
 
import java.util.Scanner;
 
public class SwapWithoutThird 
{
	 static void swap(int arg1,int arg2)
	 {
		 arg1=arg1+arg2;
		 arg2=arg1-arg2;
		 arg1=arg1-arg2;
 
		 System.out.println("after Swapin value of n1= "+arg1);
		 System.out.println("after Swapin value of n2= "+arg2);
 
	 }
 
	public static void main(String[] args) 
	{
		Scanner c=new Scanner(System.in);
		System.out.println("enter a first number");
		int n1=c.nextInt();
 
		System.out.println("enter second number");
 
        int n2=c.nextInt();
        System.out.println("value of n1= "+n1);
        System.out.println("value of n2= "+n2);
        swap(n1,n2);
 
	}
 
}
output:
enter a first number
10
enter second number
20
value of n1= 10
value of n2= 20
after Swapin value of n1= 20
after Swapin value of n2= 10

Write a program to find out Repeated characters in a string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package logic;
 
import java.util.ArrayList;
import java.util.Scanner;
 
public class Stringrepeat {
 
 
	public static void main(String[] args) {
	 System.out.println("enter the string ");
		Scanner s = new Scanner(System.in);
		String b=s.next();
 
	  for(int i=0;i<b.length()-1;i++)
	  {
		  for(int j=i;j<b.length()-1;j++)
		  { 
 
	  if(b.charAt(i)==b.charAt(j+1))
	   {
		 System.out.println("repeating chararcter is "+b.charAt(i));
	   }
	  }
	 }
	}
}
 
output:
enter the string 
aabbgjg
repeating chararcter is a
repeating chararcter is b
repeating chararcter is g

Write a program reverse number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package logic;
import java.util.Scanner;
public class Reversenumber {
 
 
	public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
        System.out.println("enter number");
 
		int n=scn.nextInt();
 
		int digit=0;
		int reverse=0;
 
		while(n!=0)
		{
			digit=n%10;
 
			n=n/10;
			reverse=reverse*10+digit;
 
 
 
		}
		System.out.println(reverse);
	}
 
}
output:
enter number
1234
4321

How to calculate sum of all numbers in a string in java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package logic;
 
import java.util.Scanner;
 
public class Stringsum {
 
 
	public static void main(String[] args)
	{
		Scanner c=new Scanner(System.in);
		System.out.println("enter a string");
		String str=c.nextLine();
		char a[]=new char[str.length()];
		int i;
 
		int sum=0;
		for(i=0;i<=str.length()-1;i++)
		{
		if(str.charAt(i)>48&&str.charAt(i)<58)
		 {
 
	     	System.out.println(str.charAt(i)-48);
	     	System.out.println('a');
	    	sum=sum+str.charAt(i)-48;
 
	      }
 
		}
 
		System.out.println("sum of number  "+ sum); 
	}
 
}
output:
enter a string
bhavesh1234
1
a
2
a
3
a
4
a
sum of number  10
  • Debabrata

    Nice programs…