Following pattern will be printed by the following code:
1
1 1
1 * 1
1 * * 1
1 1 1 1 1
Code::
import java.util.*;
public class Gndec {
public static void main(String nt[])
{
Scanner in = new Scanner(System.in);
System.out.println("Input the number of rows: ");
int n = in.nextInt();
int d=n; //for space
System.out.println("Pattern is :");
for (int i = 1; i <= n; i++)
{
for (int spc =d;spc > 0; spc--)
//print space
{
System.out.print(" ");
}
d--;
if(i==n) //last row
{
for(int j=0;j<i;j++)
{
System.out.print(1+" ");
}
}
else
{for (int j = 0; j <i; j++)
{
if (j==0||j==i-1)
{
System.out.print(1+" ");
} else
{
System.out.print("* ");
}
}
}
System.out.println();
}
}
}
Output:
1
1 1
1 * 1
1 * * 1
1 1 1 1 1
Code::
import java.util.*;
public class Gndec {
public static void main(String nt[])
{
Scanner in = new Scanner(System.in);
System.out.println("Input the number of rows: ");
int n = in.nextInt();
int d=n; //for space
System.out.println("Pattern is :");
for (int i = 1; i <= n; i++)
{
for (int spc =d;spc > 0; spc--)
//print space
{
System.out.print(" ");
}
d--;
if(i==n) //last row
{
for(int j=0;j<i;j++)
{
System.out.print(1+" ");
}
}
else
{for (int j = 0; j <i; j++)
{
if (j==0||j==i-1)
{
System.out.print(1+" ");
} else
{
System.out.print("* ");
}
}
}
System.out.println();
}
}
}
Output: