import java.util.*;
public class Ch2Q31
{
public static void main(String[] args)
{int n, k = 1;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter a integer: ");
n = kbd.nextInt();
System.out.print("Enter the pattern no(1 or 2 or 3 or 4 to Quit): ");
int choice = kbd.nextInt();
while(choice !=4)
{
switch(choice)
{case 1: pattern1(n); break;
case 2: pattern2(n); break;
case 3: pattern3(n);
}
System.out.print("Enter the pattern no(1 or 2 or 3 or 4 to Quit): ");
choice = kbd.nextInt();
}
}
static void pattern1(int n)
{for(int i = 1; i <= n; i++)
{for(int j = 1; j <= i; j++)
System.out.printf("%-3d\t", i);
System.out.println();
}
}
static void pattern2(int n)
{for(int i = 1; i <= n; i++)
{for(int j = 1; j <= i; j++)
System.out.printf("%-3d\t", j);
System.out.println();
}
}
static void pattern3(int n)
{for(int i = 1; i <= n; i++)
{for(int j = 1; j <= n+1-i; j++)
System.out.printf(" ");
for(int j = 1; j <= i; j++)
System.out.printf("%-4d\t", j);
System.out.println();
}
}
}
No comments:
Post a Comment