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.*;
import java.util.*;
import java.lang.*;
class pat1
{
public static void main(String arg[])
{
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;j++)
{
ch=st.charAt(j);
System.out.print(ch);
}
System.out.println();
}
break;
default:
System.out.println(“Wrong Choice::::”);
}
}
}

Related Posts

Comments are closed.