Examples Java 3 كورس تعلم جافا كامل بالعربي ، Examples Java 4 تعلم جافا من البداية إلى النهاية
Alphabet Pattern
Alphabet Pattern
A BB CCC DDDD EEEEE
Example
import java.util.*; class AlphabetPattern { public static void main(String[] arg) { int line, row, col; char ch = 'A'; Scanner scanner = new Scanner(System.in); System.out.print("Enter number of lines : "); line = scanner.nextInt(); for (row = 1; row <= line; row++) { for (col = 1; col <= row; col++) { System.out.print("" + ch); } System.out.println(); ch++; } } }
•Output
A BB CCC DDDD EEEEE
Binary Pattern
Binary Pattern
1 01 101 0101 10101
Example
class BinaryPattern { public static void main(String s[]) { int i, j; int count = 1; for (i = 1; i <= 5; i++) { for (j = 1; j <= i; j++) { System.out.format("%d", count++ % 2); if (j == i && i != 5) System.out.println(""); } if (i % 2 == 0) count = 1; else count = 0; } } }
•Output
1 01 101 0101 10101
Christmas tree.
Christmas tree.
Example
class ChristmasTree { public static final int segments = 4; public static final int height = 4; public static void main(String[] args) { makeTree(); } public static void makeTree() { int maxStars = 2 * height + 2 * segments - 3; String maxStr = ""; for (int l = 0; l < maxStars; l++) { maxStr += " "; } for (int i = 1; i <= segments; i++) { for (int line = 1; line <= height; line++) { String starStr = ""; for (int j = 1; j <= 2 * line + 2 * i - 3; j++) { starStr += "*"; } for (int space = 0; space <= maxStars - (height + line + i); space++) { starStr = " " + starStr; } System.out.println(starStr); } } for (int i = 0; i <= maxStars / 2; i++) { System.out.print(" "); } System.out.println(" " + "*" + " "); for (int i = 0; i <= maxStars / 2; i++) { System.out.print(" "); } System.out.println(" " + "*" + " "); for (int i = 0; i <= maxStars / 2 - 3; i++) { System.out.print(" "); } System.out.println(" " + "*******"); } }
•Output
Christmas tree pattern
Christmas tree pattern
X X XXX X XXXXX X XXX XXXXXX
Example
class ChristmasTreePattern { public static void main(String[] arg) { drawChristmasTree(4); } private static void drawChristmasTree(int n) { for (int i = 0; i < n; i++) { triangle(i + 1, n); } } private static void triangle(int n, int max) { for (int i = 0; i < n; i++) { for (int j = 0; j < max - i - 1; j++) { System.out.print(" "); } for (int j = 0; j < i * 2 + 1; j++) { System.out.print("X"); } System.out.println(""); } } }
•Output
X X XXX X XXXXX X XXX XXXXXX
Floyd Triangle
Floyd Triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Example
import java.util.Scanner; class FloydTriangle { public static void main(String args[]) { int n, num = 1, c, d; Scanner in = new Scanner(System.in); System.out.print("Enter the number of rows of floyd's triangle : "); n = in.nextInt(); System.out.println("Floyd's triangle :-"); for (c = 1; c <= n; c++) { for (d = 1; d <= c; d++) { System.out.print(num + " "); num++; } System.out.println(); } } }
•Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Number pattern 1.
Number pattern 1.
1 12 123 1234 12345
Example
class NumberPat1 { public static void main(String arg[]) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } } }
•Output
1 12 123 1234 12345
Number pattern 2.
Number pattern 2.
54321 5432 543 54 5
Example
class NumberPat2 { public static void main(String arg[]) { for (int i = 1; i <= 5; i++) { for (int j = 5; j >= i; j--) { System.out.print(j); } System.out.println(); } } }
•Output
54321 5432 543 54 5
Number pattern 3.
Number pattern 3.
12345 1234 123 12 1
Example
class NumberPat3 { public static void main(String arg[]) { for (int i = 1, r = 5; i <= 5; i++, r--) { for (int j = 1; j <= r; j++) { System.out.print(j); } System.out.println(); } } }
•Output
12345 1234 123 12 1
Number pattern 4.
Number pattern 4.
1 12 123 1234 12345 1234 123 12 1
Example
class NumberPat4 { public static void main(String arg[]) { int ck = 0, c = 2; while (c > 0) { if (ck == 0) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } ck++; } else { for (int i = 1, r = 5 - 1; i <= 5 - 1; i++, r--) { for (int j = 1; j <= r; j++) { System.out.print(j); } System.out.println(); } } c--; } } }
•Output
1 12 123 1234 12345 1234 123 12 1
Number pattern 5.
Number pattern 5.
12345 1234 123 12 1 12 123 1234 12345
Example
class NumberPat5 { public static void main(String arg[]) { int ck = 0, c = 2; while (c > 0) { if (ck == 0) { for (int i = 1, r = 5; i <= 5; i++, r--) { for (int j = 1; j <= r; j++) { System.out.print(j); } System.out.println(); } ck++; } else { for (int i = 2; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } } c--; } } }
•Output
12345 1234 123 12 1 12 123 1234 12345