Thursday, December 26, 2013

ButterFly program

package com.rrv;

import java.util.Scanner;


public class ButterFlyLogic {

public static void main(String[] args)
 {
Scanner scannerInput=new Scanner(System.in);
   //take input from user
  System.out.println("Enter Number of Steps you want: ");
int numOfSteps=scannerInput.nextInt();
  //spaceDecre variable for printing spaces
int spaceDecre=numOfSteps*2-3;
  //stepIncre variable for printing step wise
int stepIncre=1;
  //for printing top of the rows
for(int i=numOfSteps;i>1;i--)
{
   //call method steps()
ButterFlyLogic.steps(stepIncre,spaceDecre,i);
 spaceDecre=spaceDecre-2;
stepIncre++;
}
int ones=numOfSteps*2-1;
for(int i=0;i<ones;i++)
{
System.out.print("1");
}
System.out.println("\n");
  //stepIncre and spaceDecre variable for bottom steps
stepIncre=numOfSteps-1;
spaceDecre=1;
  //for printing below steps
for(int j=2;j<=numOfSteps;j++)
{
  //call method steps()
ButterFlyLogic.steps(stepIncre, spaceDecre, j);
spaceDecre=spaceDecre+2;
stepIncre--;
  }
}
  //method for printing steps
public static void steps(int stepIncre,int spaceDecre,int i)
{
int step=stepIncre;
 //printing leftside values
while(step>0)
{
System.out.print(i);
step--;
}
int space=spaceDecre;
  //printing spaces
while(space>0)
{
System.out.print(" ");
space--;
}
step=stepIncre;
  //printing rightside values
while(step>0)
{
System.out.print(i);
step--;
}
System.out.println("\n");

}

}


OUTPUT:

Enter Number of Step you want:
5


5            5
44         44
333     333
2222  2222
111111111
2222  2222
333     333
44         44
5            5

No comments:

Post a Comment