For Class 10 # Created by Prashant Tandon, Computer Teacher, SMC Kanpur # Mobile 9839575830
Answers – String Based Questions
Q4. Write a program to accept a name from the user and print the following Pattern as per
the given name. For Example, Given Name : “RITIKA”
A
A K
A K I
A K I T
A K I T I
A K I T I R
Sample Input Name : “RITIKA” ( Length : 6 )
R I T I K A
0 1 2 3 4 5
Ans. import java . util . * ;
class PatternOnString
{
public static void main( )
{
Scanner SC = new Scanner ( [Link] ) ;
[Link] ( " Enter any Name : " );
String NAME = SC . nextLine( ) ;
[Link] ( " Given Name : " + NAME );
int len, LINE, N, SP ;
char CH ;
len = [Link]( ) ;
for( LINE = len-1 ; LINE >= 0 ; LINE -- )
{
for( SP = 1 ; SP <= LINE ; SP ++ )
[Link] ( "\t" + “ ” );
for( N = len - 1 ; N > = LINE ; N -- )
{
CH = [Link]( N );
[Link] ( "\t" + CH );
} // end of inner for
[Link] ( ); // move the cursor to newline
} // end of outer for
} // end of main function
} // end of class
Page No 1 of 1