## Friday, 19 August 2016

### Chapter 5 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*5.18 (Display four patterns using loops) Use nested loops that display the following patterns in four separate programs:
Pattern A
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

Pattern B
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Pattern C
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

Pattern D

1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

import java.util.Scanner;

public class ProgrammingEx5_18A {

public static void main(String[] args) {
System.out.print("Enter the number of lines:");
Scanner input = new Scanner(System.in);

// get the total number of lines n.
int n = input.nextInt();

// Loop through the lines from 1 to n
System.out.println("Pattern A");
for (int i = 1; i <= n; i++) {

// Printing number increamentally from 1 to line number j
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();

}

}

}

import java.util.Scanner;

public class ProgrammingEx5_18B {

public static void main(String[] args) {
System.out.print("Enter the number of lines:");
Scanner input = new Scanner(System.in);

// get the total number of lines n.
int n = input.nextInt();

// Loop through the lines from 1 to n (i=0 to n-1
System.out.println("Pattern B");
for (int i = 0; i < n; i++) {

// Printing number increamentally from 1 to line number j
for (int j = 1; j <= n - i; j++) {
System.out.print(j + " ");
}
System.out.println();

}

}

}

import java.util.Scanner;

public class ProgrammingEx5_18C{

public static void main(String[] args) {
System.out.print("Enter the number of lines:");
Scanner input = new Scanner(System.in);

//get the total number of lines n.
int n = input.nextInt();

//Loop through the lines from 1 to n
System.out.println("Pattern C");
for (int i = 1; i <= n; i++) {
// printing spaces, 2 at a time from j=1 to j= n-i
for (int j = 1; j <= (n - i); j++) {
System.out.print("  ");
}

//Printing number increamentally from 1 to line number j
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();

}

}

}

import java.util.Scanner;

public class ProgrammingEx5_18D{

public static void main(String[] args) {
System.out.print("Enter the number of lines:");
Scanner input = new Scanner(System.in);

//get the total number of lines n.
int n = input.nextInt();

//Loop through the lines from 1 to n (i=0 to n-1)
System.out.println("Pattern C");
for (int i = 0; i < n; i++) {

// printing spaces, 2 at a time from j=i to 0
for (int j = i; j>0; j--) {
System.out.print("  ");
}

//Printing number increamentally from 1 to line number j
for (int j = 1; j <= n-i; j++) {
System.out.print(j + " ");
}
System.out.println();

}

}

}