Question 3.05: Write 4 programs that prompts user for the size (a non-negative integer in int); and printsthe pattern as shown:
Enter the size: 8
Solution:
A)
package com.company;
import java.util.Scanner;
public class Lab03Problem5a {
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 j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
B)
package com.company;
import java.util.Scanner;
public class Lab3Problem5b {
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=0;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" ");
}
for(int k=1;k<=n-i;k++)
{
System.out.print(k);
}
System.out.println();
}
}
}
C)
package com.company;
import java.util.Scanner;
public class Lab03Problem5c {
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++) {
int a = n - i;
for (int k = 1; k <= a; k++) {
System.out.print(" ");
}
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
}
}
D)
package com.company;
import java.util.Scanner;
public class Lab03Problem5d {
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=0;i<=n;i++)
{
int a = n-i;
for(int j=0;j< a;j++)
{
int s = (a-j);
System.out.print(s);
}
System.out.println();
}
}
}