To display the Pattern as per the User’s Choice


/*Write a program to generate a pattern of a token/word in the form of a triangle or in the form of an inverted triangle depending upon user’s choice.*/

import java.io.*;

class patt

{

Public static void main(String aa[])

{

Scanner in = new Scanner(System.in);

Int I,j,p,k;

Char ch;

String st;

System.out.println(“Enter a word as token for pattern printing:”);

St=in.next();

P=st.length();

System.out.println(“Enter your choice:”);

K=in.nextInt();

Switch(k);

{

case 1;

System.out.println(“The pattern:”);

For(i=0;i<p;i++)

{

for(j=0;j<=i;j++)

{

ch=st.charAt(j);

System.out.print(ch);

}

System.out.println();

}

break;

case 2:

System.out.println(“The pattern :”);

For(i=p;i>=1;i++)

{

for(j=0;j<i;++)

{

ch=st.charAt(j);

System.out.print(ch);

}

System.out.println();

}

break;

default:

System.out.println(“Wrong Choice::::”);

}

}

}

Output:

Enter a word as a Token    BINDU

Enter your choice 1

Enter your choice 2

B I N D U

B I N D

B I  N

B I

B

 

B

B I

B I  N

B I  N D

B I  N  D U

Related Posts

Comments are closed.