Aim:- Write an interactive program to print a string entered
in a pyramid form. For instance, the string “stream” has to be displayed as
follows:
S
S t
S t r
S t r e
S t r e a
S t r e a m
import
java.lang.*;
import
java.util.*;
class
pra9
{
public
static void main(String args[])
{
int i,j,len,k;
String str=new String();
char ch[]=new char[25];
Scanner
s=new Scanner(System.in);
System.out.print("\nEnter
your string: ");
str=s.nextLine();
len=str.length();
ch=str.toCharArray();
k=len;
for(i=len;i>=0;i--)
{
for(j=0;j<=len;j++)
{
if(i
< j)
{
System.out.print(ch[len-k] +"
");
k--;
}
else
System.out.print(" ");
}
System.out.println();
k=len;
}
}
}
Output:-
No comments:
Post a Comment