Computer Science, asked by Nik7117, 1 year ago

write a program in Java to print the following pattern

Attachments:

Answers

Answered by bond4007
1
public class Diamond
{
   public static void main(String args[])
    {
      int i, j, space = 1;
   space = 5 - 1;
        for (j = 1; j <= 5; j++)
        {
            for (i = 1; i <= space; i++)
            {
               System.out.print(" ");
            }
           space--;
for (i = 1; i <= 2 * j - 1; i++)
            {
                System.out.print("a");
            }
            System.out.println("");
       }

       space = 1;

       for (j = 1; j <= 5 - 1; j++)
        {
            for (i = 1; i <= space; i++)
            { System.out.print(" ");
           }

           space++;
for (i = 1; i <= 2 * (5 - j) - 1; i++)
            {
               System.out.print("a");
            }
System.out.println("");
 }
 }
}

Nik7117: I am sorry bro but your program is incorrect
Answered by Anonymous
0

\huge\mathfrak{Answer}

________________________________

class b2

{

static void main ( int n )

{

for (int i=0; i<n; i++)

{

// inner loop to handle number spaces

// values changing acc. to requirement

for (int j=n-i; j>1; j--)

{

// printing spaces

System.out.print(" ");

}

// inner loop to handle number of columns

// values changing acc. to outer loop

for (int k=0; k<=i; k++ )

{

System.out.print(" a ");

}

// ending line after each row

System.out.println();

}

}

// For the first triangle shaped that is :-

a

a a

a a a

a a a a

a a a a a

// For the reversed one :-

class b3

{

public static void main ( int n)

{

for (int i=0; i<n; i++)

{

for (int j=n-i; j>1; j--)

{

// printing spaces

System.out.print(" ");

}

for (int j=5; j<=i; j++ )

{

System.out.print("a");

}

System.out.println();

}

}

___________________________________

Similar questions