pattern related programs in java
Simple triangle star pattern
1)
Enter a number : 4
*
* *
* * *
* * * *
import java.util.Scanner; public class Pattern1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } } }2)
Enter a number : 4
* * * *
* * *
* *
*
import java.util.Scanner; public class Pattern2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = n; i > 0; i--) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } } }
3)
Enter a number :4
*
* *
* * *
* * * *
import java.util.Scanner; public class Pattern3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = 1; i < n; i++) { for (int k = n - 1; k > i; k--) { System.out.print(" "); } for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } } }
4)
Enter a number : 4
* * * *
* * *
* *
*
import java.util.Scanner; public class Pattern4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = n; i > 0; i--) { for (int k = n; k > i; k--) { System.out.print(" "); } for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } } }
5)
Enter a number : 5
*
***
*****
*******
*********
import java.util.Scanner; public class Pattern5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = 1; i <= n; i++) { for (int j = n; j >= i; j--) { System.out.print(" "); } for (int k = 1; k <= (2 * i - 1); k++) { System.out.print("*"); } System.out.println(""); } } }