Wednesday 3 August 2011

TO PRINT ANY CHARACTER IN ‘R’ FORMATION ACCORDING TO THE GIVEN NUMBER OF TIMES


TO PRINT ANY CHARACTER IN ‘R’ FORMATION ACCORDING TO THE GIVEN NUMBER OF TIMES
                            @ @  @
                                                       @       @
                                                      @        @
                                                       @ @  @
                                                       @ @
                                                       @      @
                                                       @          @
ALGORITHM-
Step-1 Start
Step-2 Take a character for printing and limit for printing it no. of times.
Step-3 Initialization of variables.
Step-4 Run the loop for printing the no. of lines for square.
Step-5 If loop variable is equal to first value (i.e. 0) or last value {i.e. (limit-1)}Then proceed to step 6 else to step 7.
Step-6 Run the loop for printing the first and last line of square only.
Step-7 Print the character only.
Step-8 Run the loop for giving the spaces inside the square.
Step-9 Print the character.
Step-10 Change the line.
Step-11 Run the loop for the lambda formation
Step-12 Print the character.
Step-13 Run the loop for printing the spaces.
Step-14 Print the spaces in ascending order.
Step-15 Print the character outside the space loop once again.
Step-16 Change the line.
Step-17 Exit
PROGRAM
class pattern4
{
public static void main(char ch,int n)
{
int a,b,c=0;
for (a=0;a<n;a++)
{
if (a==0||a==(n-1))
{
for (b=0;b<n;b++)
{
System.out.print (ch);
    }}
else
{
System.out.print(ch);
for (b=0;b<(n-2);b++)
{
System.out.print (" ");
    }
    System.out.print(ch);
}
System.out.println();
    }
  for (a=0;a<n;a++,c++)
{
System.out.print (ch);
for (b=0;b<c;b++)
{
System.out.print(" ");
    }
    System.out.print(ch);
    System.out.println();
}}}
  
VARIABLE DESCRIPTION-

VARIABLE USED
DATATYPE
DESCRIPTION

a

Integer type
1. For running the loop to print the no. of    lines in square formation.
2. For printing the no. of lines in lambda formation.
b
Integer type
1.   For printing the no. of spaces inside the square.
2.   For printing the no. of spaces in lambda formation
c
Integer type
Counter variable used for giving spaces in lambda formation.
n
Integer type
For storing the limit value entered by the user

 
INPUT/OUTPUT-

INPUT
OUTPUT
CHARACTER
LIMIT



A


3
AAA
A   A
AAA
                        AA
A  A
   A     A



&



4
&&&&
&      &
&      &
&&&&
                      &&
                      &   &
&     &
  &        &

No comments:

Post a Comment