Java Program Tricks


Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Trick-1


PgmTricks_Ex1.java

import java.util.Scanner;
 
public class PgmTricks_Ex1 {
	
	public static void main (String args[]) {
		
		System.out.println("Enter n Value");
		
		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());

		System.out.println();
		
		for(int i=1; i<=n ;i++) {
			
			for(int j=1; j<=i; j++) {
				
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

Sample Output

Enter n Value
5

*
**
***
****
*****




B. Trick-2


PgmTricks_Ex2.java

import java.util.Scanner;
 
public class PgmTricks_Ex2 {
	
	public static void main(String args[]) {
		
		System.out.println("Enter n Value");
		
		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());
		
		System.out.println();

		for(int i=n; i>=1; i--) {
			
			for(int j=i; j>=1; j--) {
				
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

Sample Output

Enter n Value
5

*****
****
***
**
*




C. Trick-3


PgmTricks_Ex3.java

import java.util.Scanner;
 
public class PgmTricks_Ex3 {
	
	public static void main(String args[]) {
		
		System.out.println("Enter n Value");
		
		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());

		System.out.println();
		
		for(int i=1; i<=n; i++) {
			
			for(int j=1; j<=i; j++) {
				
				System.out.print(i + " ");
			}
			System.out.println();
		}
	}
}

Sample Output

Enter n Value
5

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 




D. Trick-4


PgmTricks_Ex4.java

import java.util.Scanner;
 
public class PgmTricks_Ex4 {
	
	public static void main(String args[]) {
		
		System.out.println("Enter n Value");
		
		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());

		System.out.println();
		
		for(int i=1; i<=n; i++) {
			
			for(int j=1; j<=i; j++) {
				
				System.out.print(j + " ");
			}
			System.out.println();
		}
	}
}

Sample Output

Enter n Value
5

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 






E. Trick-5


PgmTricks_Ex5.java

import java.util.Scanner;
 
public class PgmTricks_Ex5 {
	
	public static void main(String args[]) {
		
		System.out.println("Enter n Value");
		
		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());
		
		String count = "";
		
		System.out.println();
		
		for(int i=1; i<=n; i++) 
		{
			count = count.concat("*");
		}
		
		count = count.concat(" ");
		
		for(int i=1; i<=n; i++) {
			
			for(int j=1; j<=i; j++) {
				
				System.out.print(count);
			}
			System.out.println();
		}
	}
}

Sample Output

Enter n Value
5

***** 
***** ***** 
***** ***** ***** 
***** ***** ***** ***** 
***** ***** ***** ***** ***** 




F. Trick-6


PgmTricks_Ex6.java

import java.util.Scanner;
 
public class PgmTricks_Ex6 {
	
	public static void main(String args[]) {
		
		System.out.println("Enter n Value");
		
		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());
		
		System.out.println();
		
		String count = "";
		
		for(int i=1; i<=n; i++) 
		{
			count = count.concat("*");
		}
		
		count = count.concat(" ");
		
		for(int i=1; i<=n; i++) {
			
			for(int j=1; j<=i; j++) {
				
				System.out.print(count);
			}
			System.out.println();
		}
		
		for(int i=n; i>=1; i--) {
			
			for(int j=i; j>=1; j--) {
				
				System.out.print(count);
			}
			System.out.println();
		}
	}
}

Sample Output

Enter n Value
5

***** 
***** ***** 
***** ***** ***** 
***** ***** ***** ***** 
***** ***** ***** ***** ***** 
***** ***** ***** ***** ***** 
***** ***** ***** ***** 
***** ***** ***** 
***** ***** 
***** 




G. Trick-7


PgmTricks_Ex7.java

import java.util.Scanner;
 
public class PgmTricks_Ex7 {
	
	public static void main(String args[]) {
		
		System.out.println("Enter n value :");
		
		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());
		int temp = n;
		
		for(int i=1; i<=temp; i++) {
			
			for(int space=1; space<=n; space++) {
				
				System.out.print(" ");
			}
			for(int star=1; star<=i; star++) {
				
				System.out.print("* ");
			}
			System.out.println();
			n = n-1;
		}		
	}
}

Sample Output

Enter n value :
5
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 




H. Trick-8


PgmTricks_Ex8.java

import java.util.Scanner;
 
public class PgmTricks_Ex8 {
	
	public static void main(String args[]) {
		
		System.out.println("Enter n Value :");
		
		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());
		int temp = n;
		
		System.out.println();
		
		for(int i=1; i<=temp; i++) {
			
			for(int space=1; space<=n; space++) {
				
				System.out.print(" ");
			}
			for(int star=1; star<=i; star++) {
				
				System.out.print(i +" ");
			}
			System.out.println();
			n = n-1;
		}		
	}
}

Sample Output

Enter n Value :
5

     1 
    2 2 
   3 3 3 
  4 4 4 4 
 5 5 5 5 5 




I. Trick-9


PgmTricks_Ex9.java

import java.util.Scanner;
 
public class PgmTricks_Ex9 {
	
	public static void main(String args[]) {
		
		System.out.println("Enter your name :");
		
		Scanner scan = new Scanner(System.in);
		String name = scan.nextLine();
		
		System.out.println();
		
		for(int i=0; i<name.length(); i++) {
			
			String str = name.substring(0, (i+1));
			System.out.println(str);
		}
	}
}

Sample Output

Enter your name :
Balaji

B
Ba
Bal
Bala
Balaj
Balaji




J. Trick-10


PgmTricks_Ex10.java

import java.util.Scanner;
 
public class PgmTricks_Ex10 {
	
	public static void main(String args[]) {
		
		System.out.println("Enter your name :");
		
		Scanner scan = new Scanner(System.in);
		String name = scan.nextLine();
		
		System.out.println();
		
		for(int i=0; i<name.length(); i++) {
			
			String str = name.substring(0, (name.length()-i));
			System.out.println(str);
		}
	}
}

Sample Output

Enter your name :
samplecodez

samplecodez
samplecode
samplecod
sampleco
samplec
sample
sampl
samp
sam
sa
s




SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com