Question 3.04: Write 3 programs that prompts user for the size (a non-negative integer in int); and prints the pattern as shown:
Enter the rows: 6
Solution:
A)
package com.company;
import java.util.Scanner;
public class Lab03Problem4a {
public static void main(String[] args)
{
Scanner Obj = new Scanner(System.in);
System.out.print("Enter the size: ");
int n =Obj.nextInt();
for(int i=1;i<=n;i++)
{
for(int k=1;k<=n-i;k++)
{
System.out.print(" ");
}
for(int j=1;j<i*2;j++)
{
System.out.print("#");
}
System.out.println();
}
}
}
B)
package com.company;
import java.util.Scanner;
public class Lab3Problem4b {
public static void main(String[] args)
{
Scanner Obj = new Scanner(System.in);
int n =Obj.nextInt();
for(int i=0;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" ");
}
for(int k=1;k<(n-i)*2;k++)
{
System.out.print("#");
}
System.out.println();
}
}
}