Select chapter



Java programs (2.papers)

Sponsored Links


  

common programs for interviews interview questions and answers

1
Write a program to swap two numbers without using temporary variables.
  1. public class MainClass {
    	public static void main(String[] args) {
    		int x = 50;
    		int y = 30;
    
    		System.out.println("Before swap:");
    		System.out.println("x value: "+x);
    		System.out.println("y value: "+y);
    		x = x+y;
    		y=x-y;
    		x=x-y;
    		System.out.println("After swap:");
    		System.out.println("x value: "+x);
    		System.out.println("y value: "+y);
    		int i=10, j=20;
    		j=i++ + ++i + j++ + ++i + ++j;
    		System.out.println(i);
    		System.out.println(j);
    	}
    }
Show Answer
Share Facebook gmail Twitter
2
Start pattern program : Write the logic to draw below pattern
*
**
***
****
  1. public class Triangle1 {
    	public static void main(String[] args) {
    		int num=1;
    		for(int i=0;i<4;i++)
    		{
    			for(int j=0;j<num;j++)
    			{
    				System.out.print("*");
    			}
    			num++;
    			System.out.println();
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
3
Number pattern program : Write the logic to draw below pattern
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
  1. public class Rectangle2 {
    	public static void main(String[] args) {
    		int num=1;
    		for(int i=0;i<4;i++)
    		{
    			for(int j=0;j<5;j++)
    			{
    				System.out.print(num);
    				num++;
    			}
    			num=1;
    			System.out.println();
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
4
Number pattern program : Write the logic to draw below pattern
1
2 3
4 5 6
7 8 9 10
  1. public class Triangle2 {
    	public static void main(String[] args) {
    		int num1=1;
    		int num2=1;
    		for(int i=0;i<4;i++)
    		{
    			for(int j=0;j<num1;j++)
    			{
    				System.out.print(num2);
    				num2++;
    			}
    			num1++;
    			System.out.println();
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
5
Start pattern program : Write the logic to draw below pattern
*
***
*****
***
*
  1. public class Triangle3 {
    	public static void main(String args[]) {
    		int n=1;
    		Boolean b=false;
    		for(int i=0;i<5;i++)
    		{
    			for(int j=0;j<n;j++)
    			{
    				System.out.print("*");
    				if(n==5)
    				{
    					b=true;
    				}
    			}
    			System.out.println();
    			if(b==true)
    			{
    				n=n-2;
    			}
    			else
    			{
    				n=n+2;
    			}
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
6
Number pattern program : Write the logic to draw below pattern
 1
 2 2
 3 3 3
 4 4 4 4
  1. public class Triangle4 {
    	public static void main(String[] args) {
    		int num=1;
    		for(int i=0;i<4;i++)
    		{
    			for(int j=0;j<num;j++)
    			{
    				System.out.print(num);
    			}
    			num++;
    			System.out.println();
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
7
Start pattern program : Write the logic to draw below pattern
           * 
        * * * 
      * * * * * 
    * * * * * * * 
  * * * * * * * * * 
* * * * * * * * * * * 
  1. public class Triangle5 {
    	public static void main(String[] args)
    	{
    		for (int i = 1; i <= 6; i++) {
    			for (int j = 6; j > i; j--) {
    				System.out.print("  ");
    			}
    			for (int j = i; j > 1; j--) {
    				System.out.print("*" + " ");
    			}
    			for (int j = 1; j <= i; j++) {
    				System.out.print("*" + " ");
    			}
    			System.out.println();
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
Read more papers :