Friday, 29 August 2014

How to draw a tree pattern in java

In this code we accept two input
1. A number(n) for the size of the tree
2. A character, with which we shalll draw the tree

////////////////////////////////////////////////////////////
////////// CODES STARTS
///////////////////////////////////////////////////////////

import java.util.Scanner;
public class Pattern
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String pad=" ";
        char ch= '*';
        System.out.println("ENTER NO OF LINES : ");
        int nLines=sc.nextInt();
        if(nLines%2==0){
            System.out.print("No. of line must be odd ");
            return;
        }
        int mid=(nLines+1)/2;
        for(int i=1;i<mid;i++){
            for(int j=1;j<=mid-i;j++){
                System.out.print(" "+pad);
            }
            for(int k=1; k<=2*i-1;k++){
                System.out.print(ch+pad);
            }
            System.out.println();
        }
        for(int i=mid-1; i>0; i--){
            for(int j=1; j<=mid-1;j++){
                System.out.print(" "+pad);
            }
            System.out.println(ch);
        }
    }

}

-----CODED BY SHOUVIK MITRA.

0 comments :

Post a Comment