Java mønster program forbedrer kodningsfærdigheder, logik og looping-koncepter. Det bliver mest spurgt ind Java interview at tjekke programmørens logik og tankegang. Vi kan printe en Java mønster program i forskellige designs. For at lære mønsterprogrammet skal vi have et dybt kendskab til Java loop, som f.eks til sløjfe gøre imens sløjfe. I dette afsnit vil vi lære hvordan man udskriver et mønster i Java .
Vi har klassificeret Java mønster program i tre kategorier:
Inden vi går videre til mønsterprogrammerne, lad os se fremgangsmåden.
Når du designer logik for et mønsterprogram, skal du først tegne det mønster i blokkene, som vi har vist i det følgende billede. Figuren viser et tydeligt udseende af mønsteret.
Hvert mønsterprogram har to eller flere end to sløjfer. Antallet af løkken afhænger af kompleksiteten af mønster eller logik. Den første for loop fungerer for rækken og den anden loop fungerer for kolonnen. I mønsterprogrammerne Java til loop er meget brugt.
I ovenstående mønster er række er betegnet med jeg og kolonne er betegnet med j . Vi ser, at den første række kun udskriver en stjerne. Den anden række udskriver to stjerner og så videre. Det farvet blokke udskrive mellemrum .
Lad os skabe logikken for mønsteret, giv ovenfor. I det følgende kodestykke starter vi række- og kolonneværdi fra 0. Vi kan også starte fra 1, det er dit valg.
for(int i=0; i<row; i++) { for(int j="0;" j<="i;" j++) system.out.print('* '); } system.out.println(); < pre> <p>In the above code snippet, the first for loop is for row and the second for loop for columns.</p> <p>Let's see the execution of the code step by step, for <strong>n=4</strong> (the number of rows we want to print).</p> <p> <strong>Iteration 1:</strong> </p> <pre> For i=0, 0<4 (true) for j="0," j<="0" < pre> <p>The first <strong>print</strong> statement prints a star at the first row and the second <strong>println</strong> statement prints the spaces and throws the cursor at the next line.</p> <pre> * </pre> <p>Now the value of i and j is increased to 1.</p> <p> <strong>Iteration 2:</strong> </p> <pre> For i=1, 1<4 (true) for j="1," 1<="1" < pre> <p>The first <strong>print</strong> statement prints two stars at the second row and the second <strong>println</strong> statement prints the spaces and throws the cursor at the next line.</p> <pre> * * * </pre> <p>Now the value of i and j is increased to 2.</p> <p> <strong>Iteration 3:</strong> </p> <pre> For i=2, 2<4 (true) for j="2," 2<="2" < pre> <p>The first <strong>print</strong> statement prints three stars at the third row and the second <strong>println</strong> statement prints the spaces and throws the cursor at the next line.</p> <pre> * * * * * * </pre> <p>Now the value of i and j is increased to 3.</p> <p> <strong>Iteration 4:</strong> </p> <pre> For i=3, 3<4 (true) for j="3," 3<="3" < pre> <p>The first <strong>print</strong> statement prints four stars at the fourth row and the second <strong>println</strong> statement prints the spaces and throws the cursor at the next line.</p> <pre> * * * * * * * * * * </pre> <p>Now the value of i and j is increased to 4.</p> <pre> For i=4, 4<4 (false) < pre> <p>The execution of the program will terminate when the value of i will be equal to the number of rows.</p> <h2>Star Pattern</h2> <p> <strong>1. Right Triangle Star Pattern</strong> </p> <pre> public class RightTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row=6; //outer loop for rows for(i=0; i<row; i++) { inner loop for columns for(j="0;" j<="i;" j++) prints stars system.out.print('* '); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-2.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Left Triangle Star Pattern</strong> </p> <pre> public class LeftTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row = 6; //Outer loop work for rows for (i=0; i=0; j--) { //prints space between two stars System.out.print(' '); } //inner loop for columns for (j=0; j<=i; j++ ) { prints star system.out.print('* '); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-3.webp" alt="How to Print Pattern in Java"> <p> <strong>3. Pyramid Star Pattern</strong> </p> <pre> public class PyramidPattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row = 6; //Outer loop work for rows for (i=0; i1; j--) { //prints space between two stars System.out.print(' '); } //inner loop for columns for (j=0; j<=i; j++ ) { prints star system.out.print('* '); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-4.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Diamond Shape Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondPattern { public static void main(String args[]) { int row, i, j, space = 1; System.out.print('Enter the number of rows you want to print: '); Scanner sc = new Scanner(System.in); row = sc.nextInt(); space = row - 1; for (j = 1; j<= row; j++) { for (i="1;" i<="space;" i++) system.out.print(' '); } space--; i <="2" * j - 1; system.out.print('*'); system.out.println(''); space="1;" (j="1;" j<="row" space++; (row j) pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-5.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Downward Triangle Star Pattern</strong> </p> <pre> public class DownwardTrianglePattern { public static void main(String[] args) { int rows=7; //inner loop for (int i= rows-1; i>=0 ; i--) { //outer loop for (int j=0; j<=i; j++) { prints star and space system.out.print('*' + ' '); } throws the cursor in next line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-6.webp" alt="How to Print Pattern in Java"> <p> <strong>6. Mirrored Right Triangle Star Pattern</strong> </p> <pre> public class MirroredRightTrianglePattern { public static void main(String[] args) { int n=7; //inner loop for (int i= 0; i<= n; i++) { outer loop for (int j="1;" j<="n-i;" j++) system.out.print(' '); } k="0;k<=i;k++)" system.out.print('*'); system.out.println(''); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-7.webp" alt="How to Print Pattern in Java"> <p> <strong>7. Reverse Pyramid Star Pattern</strong> </p> <pre> public class ReversePyramidPattern { public static void main(String[] args) { int rows=8; for (int i= 0; i<= rows-1; i++) { for (int j="0;" j<="i;" j++) system.out.print(' '); } k="0;" k<="rows-1-i;" k++) system.out.print('*' + ' system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-8.webp" alt="How to Print Pattern in Java"> <p> <strong>8. Right Down Mirror Star Pattern</strong> </p> <pre> public class RightDownMirrorPattern { public static void main(String args[]) { int row=7; for (int i= row; i>= 1; i--) { for (int j=row; j>i;j--) { System.out.print(' '); } for (int k=1;k<=i;k++) { system.out.print('*'); } system.out.println(''); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-9.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Right Pascal's Triangle</strong> </p> <pre> import java.util.Scanner; public class RightPascalTrianglePattern { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i= 0; i<= rows-1; i++) { for (j="0;" j<="i;" j++) system.out.print('*'+ ' '); } system.out.println(''); (i="rows-1;" i>=0; i--) { for(j=0; j <= i-1;j++) { system.out.print('*'+ ' '); } system.out.println(''); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-10.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Left Pascal's Triangle</strong> </p> <pre> import java.util.Scanner; public class LeftPascalTrianglePattern { public static void main(String[] args) { int i, j, k, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i= 1; i<= rows ; i++) { for (j="i;" j <rows ;j++) system.out.print(' '); } (k="1;" k<="i;k++)" system.out.print('*'); system.out.println(''); (i="rows;" i>=1; i--) { for(j=i; j<=rows;j++) { system.out.print(' '); } for(k="1;" k <i ;k++) system.out.print('*'); system.out.println(''); sc.close(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-11.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Sandglass Star Pattern</strong> </p> <pre> import java.util.Scanner; public class SandglassPattern { public static void main(String[] args) { int i, j, k, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i= 0; i<= n-1 ; i++) { for (j="0;" j <i; j++) system.out.print(' '); } (k="i;" k<="n-1;" k++) system.out.print('*' + ' system.out.println(''); (i="n-1;" i>= 0; i--) { for (j=0; j <i; j++) { system.out.print(' '); } for (k="i;" k<="n-1;" k++) system.out.print('*' + ' system.out.println(''); sc.close(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-12.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Alphabet Star Pattern</strong> </p> <pre> import java.util.*; public class AlphabetPattern { public static void main(String[] args) { int i, j, n=8; // Outer for loop for number of lines for (i = 0; i<=n; i++) { inner for loop logic execution (j="0;" j<="n" 2; j++) prints middle line i="=" n 2) system.out.print('*'); else system.out.print(' '); system.out.println(); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-13.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Triangle Star Pattern</strong> </p> <pre> import java.util.Scanner; public class TrianglePattern { public static void main(String[] args) { int i, j, k, rows=9; for (i=1; i<= rows ; i++) { for (j="i;" j < j++) system.out.print(' '); } (k="1;" k -1) ;k++) if(k="=1" || i="=" system.out.print('*'); else system.out.println(''); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-14.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Down Triangle Pattern</strong> </p> <pre> import java.util.Scanner; public class DownTrianglePattern { public static void main(String[] args) { int i, j, k, rows=9; for (i=rows; i>= 1 ; i--) { for (j = i; j<rows ; j++) { system.out.print(' '); } for (k="1;" k <="(2*i" -1) ;k++) if( || i="=" rows system.out.print('*'); else system.out.println(''); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-15.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Diamond Star Pattern</strong> </p> <pre> import java.util.*; public class DiamondPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println('Enter the number of rows you want to print: '); int rows = sc.nextInt(); for (i=1; i i ; j--) { System.out.print(' '); } System.out.print('*'); for (k = 1; k <2*(i -1) ;k++) { system.out.print(' '); } if( i="=1)" system.out.println(''); else system.out.println('*'); for (i="rows-1;">= 1 ; i--) { for (int j = rows; j > i ; j--) { System.out.print(' '); } System.out.print('*'); for (int k = 1; k <2*(i -1) ;k++) { system.out.print(' '); } if(i="=1)" system.out.println(''); else system.out.println('*'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-16.webp" alt="How to Print Pattern in Java"> <h2>Number Pattern</h2> <p> <strong>1. Pattern-1</strong> </p> <pre> public class Pattern1 { public static void main(String args[]) { int i, j,number, n=7; //loop for rows for(i=0; i<n; i++) { number="1;" loop for columns for(j="0;" j<="i;" j++) prints num system.out.print(number+ ' '); incrementing the value of number++; } throws cursor at next line after printing each row system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-17.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Pattern-2</strong> </p> <pre> public class Pattern2 { public static void main(String[] args) { int i, j, k = 1; //inner loop for (i = 1; i <= 7; i++) { outer loop for (j="1;" j< i + 1; j++) prints the value of k system.out.print(k++ ' '); } throws cursor at next line system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-18.webp" alt="How to Print Pattern in Java"> <p> <strong>3. Pattern-3</strong> </p> <pre> public class Pattern3 { public static void main(String[] args) { int n = 8; //n is the number of rows you want to print for (int i = 0; i <n; 2 i++) { int number="1;" system.out.printf('%' + (n - i) * 's', ''); for (int j="0;" <="i;" j++) system.out.printf('%4d', number); (i j) (j 1); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-19.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Pattern-4</strong> </p> <pre> public class Pattern4 { public static void main(String[] args) { for (int i = 1; i <= 4; i++) { int n="8;" for (int j="1;" k--) system.out.print(k); } l="2;" <="i;" l++) system.out.print(l); system.out.println(); i="3;">= 1; i--) { int n = 10; for (int j = 0; j= 1; k--) { System.out.print(k); } for (int l = 2; l <= i; l++) { system.out.print(l); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-20.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Pattern-5</strong> </p> <pre> import java.util.*; public class Pattern5 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) system.out.print(i+' '); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-21.webp" alt="How to Print Pattern in Java"> <p> <strong>6. Pattern-6</strong> </p> <pre> import java.util.*; public class Pattern6 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows youy want to print: '); rows = sc.nextInt(); for (i = rows; i >= 1; i--) { for (j = rows; j >= i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-22.webp" alt="How to Print Pattern in Java"> <p> <strong>7. Pattern-7</strong> </p> <pre> import java.util.Scanner; public class Pattern7 { public static void main(String[] args) { int i, j, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-23.webp" alt="How to Print Pattern in Java"> <p> <strong>8. Pattern-8</strong> </p> <pre> public class Pattern8 { public static void main(String[] args) { int rows=9; //number of rows to print for (int i = 1; i <= 1 rows; i++) { int num; if(i%2="=" 0) num="0;" for (int j="1;" <="rows;" j++) system.out.print(num); =="0)?" : 0; } else system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-24.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Pattern-9</strong> </p> <pre> import java.util.Scanner; public class Pattern9 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) if(j%2="=" 0) system.out.print(0); } else system.out.print(1); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-25.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Pattern-10</strong> </p> <pre> public class Pattern10 { public static void main(String[] args) { int n = 10; for (int i = 1; i <= n; i++) { for (int j="1;" < i; j++) system.out.print(' '); } k="i;" k++) system.out.print(k+' system.out.println(); i="n-1;">= 1; i--) { for (int j = 1; j <i; j++) { system.out.print(' '); } for (int k="i;" <="n;" k++) system.out.print(k+' system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-26.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Pattern-11</strong> </p> <pre> public class Pattern11 { public static void main(String[] args) { int rows=8; //Prints upper half pattern for (int i = 1; i <= rows; i++) { for (int j="1;" <="i;" j++) system.out.print(j+' '); } system.out.println(); prints lower half pattern i="rows-1;">= 1; i--) { for (int j = 1; j <= i; j++) { system.out.print(j+' '); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-27.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Pattern-12</strong> </p> <pre> public class Pattern12 { public static void main(String[] args) { int rows=9; for (int i = 1; i = i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-28.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Pattern-13</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int i, j, rows=9; for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+' '); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-29.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Pattern-14</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int rows=7, i, j; for (i = rows; i >= 1; i--) { for (j = i; j >= 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-30.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Pattern-15</strong> </p> <pre> public class Pattern15 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { prints first half of the row for (j="1;" j="1;" j--) system.out.print(j+' '); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-31.webp" alt="How to Print Pattern in Java"> <p> <strong>16. Pattern-16</strong> </p> <pre> public class Pattern16 { public static void main(String[] args) { int i, j, rows=9; //Prints upper half pattern for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+' '); } system.out.println(); prints lower half pattern for (i="2;" i <="rows;" i++) (j="1;" j pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-32.webp" alt="How to Print Pattern in Java"> <p> <strong>17. Pattern-17</strong> </p> <pre> public class Pattern17 { public static void main(String[] args) { int rows=9; //Prints upper half pattern for (int i = 1; i <= rows; i++) { prints i spaces at the beginning of each row for (int j="1;" < i; j++) system.out.print(' '); } to rows value end system.out.print(j); system.out.println(); lower half pattern>= 1; i--) { //Prints i spaces at the beginning of each row for (int j = 1; j <i; j++) { system.out.print(' '); } prints i to rows value at the end of each row for (int j="i;" <="rows;" system.out.print(j); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-33.webp" alt="How to Print Pattern in Java"> <p> <strong>18. Pattern-18</strong> </p> <pre> public class Pattern18 { public static void main(String[] args) { int rows=8; for (int i = 1; i <= rows; i++) { for (int j="1;" <="rows-i;" j++) system.out.print(1); } system.out.print(i); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-34.webp" alt="How to Print Pattern in Java"> <p> <strong>19. Pattern-19</strong> </p> <pre> public class Pattern19 { public static void main(String args[]) { int rows=9; for (int i = 1; i <= rows; i++) { int num="i;" for (int j="1;" <="i;" j++) system.out.print(num+' '); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-35.webp" alt="How to Print Pattern in Java"> <p> <strong>20. Pattern-20</strong> </p> <pre> public class Pattern20 { public static void main(String[] args) { int i, j, k, rows=9; for(i=1;i<rows+1 ;i++) { for(j="i;" j < rows+1 ;j++) system.out.print(j + ' '); } for(k="1;" k i ;k++) system.out.print(k system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-36.webp" alt="How to Print Pattern in Java"> <p> <strong>21. Pattern-21</strong> </p> <pre> import java.util.Scanner; public class Pattern21 { public static void main(String[] args) { int i, j, min, n; //n is the number up to you want to print System.out.print('Enter the value of n: '); Scanner sc= new Scanner(System.in); n=sc.nextInt(); //loo for upper left part for (i = 1; i <= n; i++) { for (j="1;" j <="n;" j++) min="i" <j ? i : j; system.out.print(n - + 1+ ' '); } loop upper right part 1;>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ ' '); } system.out.println(); loop for lower left part (i="n" 1;>= 1; i--) { for (j = 1; j <= n; j++) { min="i" <j ? i : j; system.out.print(n - + 1+ ' '); } loop for lower right part (j="n" 1; j>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ ' '); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-37.webp" alt="How to Print Pattern in Java"> <h2>Character Pattern</h2> <p> <strong>1. Right Triangle Alphabetic Pattern</strong> </p> <pre> public class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 //inner loop for rows for (int i = 0; i <= 8; i++) { outer loop for columns (int j="0;" <="i;" j++) adds the value of in ascii a and prints corresponding alphabet system.out.print((char) (alphabet + j) ' '); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-38.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Repeating Alphabet Pattern</strong> </p> <pre> public class RepeatingPattern { public static void main(String[] args) { int letter = 65; //ASCII value of capital A is 65 //inner loop for rwos for (int i = 0; i<= 9; i++) { outer loop for columns (int j="0;" <="i;" j++) prints the character system.out.print((char) letter + ' '); } letter++; system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-39.webp" alt="How to Print Pattern in Java"> <p> <strong>3. K-shape Alphabet Pattern</strong> </p> <pre> public class KshapePattern { public static void main(String[] args) { for (int i = 8; i >= 0; i--) { int alphabet = 65; for (int j = 0; j <= i; j++) { system.out.print((char) (alphabet + j) ' '); } system.out.println(); for (int i="0;" i<="8;" i++) int alphabet="65;" j="0;" <="i;" pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-40.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Triangle Character Pattern</strong> </p> <pre> public class TriangleCharacterPattern { public static void main(String[] args) { for (int i = 0; i i; j--) { System.out.print(' '); } for (int k = 0; k <= i; k++) { system.out.print((char) (alphabet + k) ' '); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-41.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Diamond Character Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondCharacterPattern { public static void main(String[] args) { char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print('Enter a Character between A to Z : '); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next('[A-Z]').charAt(0); // search for letter number in the array letter for (int i = 0; i <alphabet.length; i++) { if (letter[i]="=" user_ alphabet) alphabet _number="i;" break; } construct diamond for (int i="0;" <="alphabet" _number; diamond[i] ; add initial spaces j="0;" - i; j++) +=" " space between letters (alphabet[i] !="A" ) <2 * 1; draw the first part of system.out.println(diamond[i]);>= 0; i--) { // Draw the second part of the diamond // prints the diamondArray in the reverse order System.out.println(diamond[i]); } } catch (Exception e) { e.printStackTrace(); } finally { reader.close(); } } } </alphabet.length;></pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-42.webp" alt="How to Print Pattern in Java"> <hr></=></pre></=></pre></=></pre></=></pre></j></=></j></=></pre></rows+1></pre></=></pre></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></n;></pre></=></pre></n;></pre></2*(i></2*(i></pre></rows></pre></=></pre></=n;></pre></i;></=></pre></=rows;j++)></=></pre></=></=></pre></=i;k++)></pre></=></pre></=></pre></=i;></pre></=></pre></=i;></pre></=i;></pre></row;></pre></4></pre></4></pre></4></pre></4></pre></4></pre></row;>
Nu er værdien af i og j øget til 1.
Gentagelse 2:
For i=1, 1<4 (true) for j="1," 1<="1" < pre> <p>The first <strong>print</strong> statement prints two stars at the second row and the second <strong>println</strong> statement prints the spaces and throws the cursor at the next line.</p> <pre> * * * </pre> <p>Now the value of i and j is increased to 2.</p> <p> <strong>Iteration 3:</strong> </p> <pre> For i=2, 2<4 (true) for j="2," 2<="2" < pre> <p>The first <strong>print</strong> statement prints three stars at the third row and the second <strong>println</strong> statement prints the spaces and throws the cursor at the next line.</p> <pre> * * * * * * </pre> <p>Now the value of i and j is increased to 3.</p> <p> <strong>Iteration 4:</strong> </p> <pre> For i=3, 3<4 (true) for j="3," 3<="3" < pre> <p>The first <strong>print</strong> statement prints four stars at the fourth row and the second <strong>println</strong> statement prints the spaces and throws the cursor at the next line.</p> <pre> * * * * * * * * * * </pre> <p>Now the value of i and j is increased to 4.</p> <pre> For i=4, 4<4 (false) < pre> <p>The execution of the program will terminate when the value of i will be equal to the number of rows.</p> <h2>Star Pattern</h2> <p> <strong>1. Right Triangle Star Pattern</strong> </p> <pre> public class RightTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row=6; //outer loop for rows for(i=0; i<row; i++) { inner loop for columns for(j="0;" j<="i;" j++) prints stars system.out.print(\'* \'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-2.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Left Triangle Star Pattern</strong> </p> <pre> public class LeftTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row = 6; //Outer loop work for rows for (i=0; i=0; j--) { //prints space between two stars System.out.print(' '); } //inner loop for columns for (j=0; j<=i; j++ ) { prints star system.out.print(\'* \'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-3.webp" alt="How to Print Pattern in Java"> <p> <strong>3. Pyramid Star Pattern</strong> </p> <pre> public class PyramidPattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row = 6; //Outer loop work for rows for (i=0; i1; j--) { //prints space between two stars System.out.print(' '); } //inner loop for columns for (j=0; j<=i; j++ ) { prints star system.out.print(\'* \'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-4.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Diamond Shape Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondPattern { public static void main(String args[]) { int row, i, j, space = 1; System.out.print('Enter the number of rows you want to print: '); Scanner sc = new Scanner(System.in); row = sc.nextInt(); space = row - 1; for (j = 1; j<= row; j++) { for (i="1;" i<="space;" i++) system.out.print(\' \'); } space--; i <="2" * j - 1; system.out.print(\'*\'); system.out.println(\'\'); space="1;" (j="1;" j<="row" space++; (row j) pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-5.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Downward Triangle Star Pattern</strong> </p> <pre> public class DownwardTrianglePattern { public static void main(String[] args) { int rows=7; //inner loop for (int i= rows-1; i>=0 ; i--) { //outer loop for (int j=0; j<=i; j++) { prints star and space system.out.print(\'*\' + \' \'); } throws the cursor in next line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-6.webp" alt="How to Print Pattern in Java"> <p> <strong>6. Mirrored Right Triangle Star Pattern</strong> </p> <pre> public class MirroredRightTrianglePattern { public static void main(String[] args) { int n=7; //inner loop for (int i= 0; i<= n; i++) { outer loop for (int j="1;" j<="n-i;" j++) system.out.print(\' \'); } k="0;k<=i;k++)" system.out.print(\'*\'); system.out.println(\'\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-7.webp" alt="How to Print Pattern in Java"> <p> <strong>7. Reverse Pyramid Star Pattern</strong> </p> <pre> public class ReversePyramidPattern { public static void main(String[] args) { int rows=8; for (int i= 0; i<= rows-1; i++) { for (int j="0;" j<="i;" j++) system.out.print(\' \'); } k="0;" k<="rows-1-i;" k++) system.out.print(\'*\' + \' system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-8.webp" alt="How to Print Pattern in Java"> <p> <strong>8. Right Down Mirror Star Pattern</strong> </p> <pre> public class RightDownMirrorPattern { public static void main(String args[]) { int row=7; for (int i= row; i>= 1; i--) { for (int j=row; j>i;j--) { System.out.print(' '); } for (int k=1;k<=i;k++) { system.out.print(\'*\'); } system.out.println(\'\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-9.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Right Pascal's Triangle</strong> </p> <pre> import java.util.Scanner; public class RightPascalTrianglePattern { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i= 0; i<= rows-1; i++) { for (j="0;" j<="i;" j++) system.out.print(\'*\'+ \' \'); } system.out.println(\'\'); (i="rows-1;" i>=0; i--) { for(j=0; j <= i-1;j++) { system.out.print(\'*\'+ \' \'); } system.out.println(\'\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-10.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Left Pascal's Triangle</strong> </p> <pre> import java.util.Scanner; public class LeftPascalTrianglePattern { public static void main(String[] args) { int i, j, k, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i= 1; i<= rows ; i++) { for (j="i;" j <rows ;j++) system.out.print(\' \'); } (k="1;" k<="i;k++)" system.out.print(\'*\'); system.out.println(\'\'); (i="rows;" i>=1; i--) { for(j=i; j<=rows;j++) { system.out.print(\' \'); } for(k="1;" k <i ;k++) system.out.print(\'*\'); system.out.println(\'\'); sc.close(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-11.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Sandglass Star Pattern</strong> </p> <pre> import java.util.Scanner; public class SandglassPattern { public static void main(String[] args) { int i, j, k, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i= 0; i<= n-1 ; i++) { for (j="0;" j <i; j++) system.out.print(\' \'); } (k="i;" k<="n-1;" k++) system.out.print(\'*\' + \' system.out.println(\'\'); (i="n-1;" i>= 0; i--) { for (j=0; j <i; j++) { system.out.print(\' \'); } for (k="i;" k<="n-1;" k++) system.out.print(\'*\' + \' system.out.println(\'\'); sc.close(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-12.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Alphabet Star Pattern</strong> </p> <pre> import java.util.*; public class AlphabetPattern { public static void main(String[] args) { int i, j, n=8; // Outer for loop for number of lines for (i = 0; i<=n; i++) { inner for loop logic execution (j="0;" j<="n" 2; j++) prints middle line i="=" n 2) system.out.print(\'*\'); else system.out.print(\' \'); system.out.println(); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-13.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Triangle Star Pattern</strong> </p> <pre> import java.util.Scanner; public class TrianglePattern { public static void main(String[] args) { int i, j, k, rows=9; for (i=1; i<= rows ; i++) { for (j="i;" j < j++) system.out.print(\' \'); } (k="1;" k -1) ;k++) if(k="=1" || i="=" system.out.print(\'*\'); else system.out.println(\'\'); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-14.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Down Triangle Pattern</strong> </p> <pre> import java.util.Scanner; public class DownTrianglePattern { public static void main(String[] args) { int i, j, k, rows=9; for (i=rows; i>= 1 ; i--) { for (j = i; j<rows ; j++) { system.out.print(\' \'); } for (k="1;" k <="(2*i" -1) ;k++) if( || i="=" rows system.out.print(\'*\'); else system.out.println(\'\'); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-15.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Diamond Star Pattern</strong> </p> <pre> import java.util.*; public class DiamondPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println('Enter the number of rows you want to print: '); int rows = sc.nextInt(); for (i=1; i i ; j--) { System.out.print(' '); } System.out.print('*'); for (k = 1; k <2*(i -1) ;k++) { system.out.print(\' \'); } if( i="=1)" system.out.println(\'\'); else system.out.println(\'*\'); for (i="rows-1;">= 1 ; i--) { for (int j = rows; j > i ; j--) { System.out.print(' '); } System.out.print('*'); for (int k = 1; k <2*(i -1) ;k++) { system.out.print(\' \'); } if(i="=1)" system.out.println(\'\'); else system.out.println(\'*\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-16.webp" alt="How to Print Pattern in Java"> <h2>Number Pattern</h2> <p> <strong>1. Pattern-1</strong> </p> <pre> public class Pattern1 { public static void main(String args[]) { int i, j,number, n=7; //loop for rows for(i=0; i<n; i++) { number="1;" loop for columns for(j="0;" j<="i;" j++) prints num system.out.print(number+ \' \'); incrementing the value of number++; } throws cursor at next line after printing each row system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-17.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Pattern-2</strong> </p> <pre> public class Pattern2 { public static void main(String[] args) { int i, j, k = 1; //inner loop for (i = 1; i <= 7; i++) { outer loop for (j="1;" j< i + 1; j++) prints the value of k system.out.print(k++ \' \'); } throws cursor at next line system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-18.webp" alt="How to Print Pattern in Java"> <p> <strong>3. Pattern-3</strong> </p> <pre> public class Pattern3 { public static void main(String[] args) { int n = 8; //n is the number of rows you want to print for (int i = 0; i <n; 2 i++) { int number="1;" system.out.printf(\'%\' + (n - i) * \'s\', \'\'); for (int j="0;" <="i;" j++) system.out.printf(\'%4d\', number); (i j) (j 1); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-19.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Pattern-4</strong> </p> <pre> public class Pattern4 { public static void main(String[] args) { for (int i = 1; i <= 4; i++) { int n="8;" for (int j="1;" k--) system.out.print(k); } l="2;" <="i;" l++) system.out.print(l); system.out.println(); i="3;">= 1; i--) { int n = 10; for (int j = 0; j= 1; k--) { System.out.print(k); } for (int l = 2; l <= i; l++) { system.out.print(l); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-20.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Pattern-5</strong> </p> <pre> import java.util.*; public class Pattern5 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) system.out.print(i+\' \'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-21.webp" alt="How to Print Pattern in Java"> <p> <strong>6. Pattern-6</strong> </p> <pre> import java.util.*; public class Pattern6 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows youy want to print: '); rows = sc.nextInt(); for (i = rows; i >= 1; i--) { for (j = rows; j >= i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-22.webp" alt="How to Print Pattern in Java"> <p> <strong>7. Pattern-7</strong> </p> <pre> import java.util.Scanner; public class Pattern7 { public static void main(String[] args) { int i, j, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-23.webp" alt="How to Print Pattern in Java"> <p> <strong>8. Pattern-8</strong> </p> <pre> public class Pattern8 { public static void main(String[] args) { int rows=9; //number of rows to print for (int i = 1; i <= 1 rows; i++) { int num; if(i%2="=" 0) num="0;" for (int j="1;" <="rows;" j++) system.out.print(num);="="0)?"" : 0; } else system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-24.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Pattern-9</strong> </p> <pre> import java.util.Scanner; public class Pattern9 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) if(j%2="=" 0) system.out.print(0); } else system.out.print(1); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-25.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Pattern-10</strong> </p> <pre> public class Pattern10 { public static void main(String[] args) { int n = 10; for (int i = 1; i <= n; i++) { for (int j="1;" < i; j++) system.out.print(\' \'); } k="i;" k++) system.out.print(k+\' system.out.println(); i="n-1;">= 1; i--) { for (int j = 1; j <i; j++) { system.out.print(\' \'); } for (int k="i;" <="n;" k++) system.out.print(k+\' system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-26.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Pattern-11</strong> </p> <pre> public class Pattern11 { public static void main(String[] args) { int rows=8; //Prints upper half pattern for (int i = 1; i <= rows; i++) { for (int j="1;" <="i;" j++) system.out.print(j+\' \'); } system.out.println(); prints lower half pattern i="rows-1;">= 1; i--) { for (int j = 1; j <= i; j++) { system.out.print(j+\' \'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-27.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Pattern-12</strong> </p> <pre> public class Pattern12 { public static void main(String[] args) { int rows=9; for (int i = 1; i = i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-28.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Pattern-13</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int i, j, rows=9; for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\' \'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-29.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Pattern-14</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int rows=7, i, j; for (i = rows; i >= 1; i--) { for (j = i; j >= 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-30.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Pattern-15</strong> </p> <pre> public class Pattern15 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { prints first half of the row for (j="1;" j="1;" j--) system.out.print(j+\' \'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-31.webp" alt="How to Print Pattern in Java"> <p> <strong>16. Pattern-16</strong> </p> <pre> public class Pattern16 { public static void main(String[] args) { int i, j, rows=9; //Prints upper half pattern for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\' \'); } system.out.println(); prints lower half pattern for (i="2;" i <="rows;" i++) (j="1;" j pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-32.webp" alt="How to Print Pattern in Java"> <p> <strong>17. Pattern-17</strong> </p> <pre> public class Pattern17 { public static void main(String[] args) { int rows=9; //Prints upper half pattern for (int i = 1; i <= rows; i++) { prints i spaces at the beginning of each row for (int j="1;" < i; j++) system.out.print(\' \'); } to rows value end system.out.print(j); system.out.println(); lower half pattern>= 1; i--) { //Prints i spaces at the beginning of each row for (int j = 1; j <i; j++) { system.out.print(\' \'); } prints i to rows value at the end of each row for (int j="i;" <="rows;" system.out.print(j); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-33.webp" alt="How to Print Pattern in Java"> <p> <strong>18. Pattern-18</strong> </p> <pre> public class Pattern18 { public static void main(String[] args) { int rows=8; for (int i = 1; i <= rows; i++) { for (int j="1;" <="rows-i;" j++) system.out.print(1); } system.out.print(i); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-34.webp" alt="How to Print Pattern in Java"> <p> <strong>19. Pattern-19</strong> </p> <pre> public class Pattern19 { public static void main(String args[]) { int rows=9; for (int i = 1; i <= rows; i++) { int num="i;" for (int j="1;" <="i;" j++) system.out.print(num+\' \'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-35.webp" alt="How to Print Pattern in Java"> <p> <strong>20. Pattern-20</strong> </p> <pre> public class Pattern20 { public static void main(String[] args) { int i, j, k, rows=9; for(i=1;i<rows+1 ;i++) { for(j="i;" j < rows+1 ;j++) system.out.print(j + \' \'); } for(k="1;" k i ;k++) system.out.print(k system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-36.webp" alt="How to Print Pattern in Java"> <p> <strong>21. Pattern-21</strong> </p> <pre> import java.util.Scanner; public class Pattern21 { public static void main(String[] args) { int i, j, min, n; //n is the number up to you want to print System.out.print('Enter the value of n: '); Scanner sc= new Scanner(System.in); n=sc.nextInt(); //loo for upper left part for (i = 1; i <= n; i++) { for (j="1;" j <="n;" j++) min="i" <j ? i : j; system.out.print(n - + 1+ \' \'); } loop upper right part 1;>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \' \'); } system.out.println(); loop for lower left part (i="n" 1;>= 1; i--) { for (j = 1; j <= n; j++) { min="i" <j ? i : j; system.out.print(n - + 1+ \' \'); } loop for lower right part (j="n" 1; j>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \' \'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-37.webp" alt="How to Print Pattern in Java"> <h2>Character Pattern</h2> <p> <strong>1. Right Triangle Alphabetic Pattern</strong> </p> <pre> public class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 //inner loop for rows for (int i = 0; i <= 8; i++) { outer loop for columns (int j="0;" <="i;" j++) adds the value of in ascii a and prints corresponding alphabet system.out.print((char) (alphabet + j) \' \'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-38.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Repeating Alphabet Pattern</strong> </p> <pre> public class RepeatingPattern { public static void main(String[] args) { int letter = 65; //ASCII value of capital A is 65 //inner loop for rwos for (int i = 0; i<= 9; i++) { outer loop for columns (int j="0;" <="i;" j++) prints the character system.out.print((char) letter + \' \'); } letter++; system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-39.webp" alt="How to Print Pattern in Java"> <p> <strong>3. K-shape Alphabet Pattern</strong> </p> <pre> public class KshapePattern { public static void main(String[] args) { for (int i = 8; i >= 0; i--) { int alphabet = 65; for (int j = 0; j <= i; j++) { system.out.print((char) (alphabet + j) \' \'); } system.out.println(); for (int i="0;" i<="8;" i++) int alphabet="65;" j="0;" <="i;" pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-40.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Triangle Character Pattern</strong> </p> <pre> public class TriangleCharacterPattern { public static void main(String[] args) { for (int i = 0; i i; j--) { System.out.print(' '); } for (int k = 0; k <= i; k++) { system.out.print((char) (alphabet + k) \' \'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-41.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Diamond Character Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondCharacterPattern { public static void main(String[] args) { char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print('Enter a Character between A to Z : '); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next('[A-Z]').charAt(0); // search for letter number in the array letter for (int i = 0; i <alphabet.length; i++) { if (letter[i]="=" user_ alphabet) alphabet _number="i;" break; } construct diamond for (int i="0;" <="alphabet" _number; diamond[i] ; add initial spaces j="0;" - i; j++) +=" " space between letters (alphabet[i] !="A" ) <2 * 1; draw the first part of system.out.println(diamond[i]);>= 0; i--) { // Draw the second part of the diamond // prints the diamondArray in the reverse order System.out.println(diamond[i]); } } catch (Exception e) { e.printStackTrace(); } finally { reader.close(); } } } </alphabet.length;></pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-42.webp" alt="How to Print Pattern in Java"> <hr></=></pre></=></pre></=></pre></=></pre></j></=></j></=></pre></rows+1></pre></=></pre></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></n;></pre></=></pre></n;></pre></2*(i></2*(i></pre></rows></pre></=></pre></=n;></pre></i;></=></pre></=rows;j++)></=></pre></=></=></pre></=i;k++)></pre></=></pre></=></pre></=i;></pre></=></pre></=i;></pre></=i;></pre></row;></pre></4></pre></4></pre></4></pre></4>
Nu er værdien af i og j øget til 2.
Gentagelse 3:
For i=2, 2<4 (true) for j="2," 2<="2" < pre> <p>The first <strong>print</strong> statement prints three stars at the third row and the second <strong>println</strong> statement prints the spaces and throws the cursor at the next line.</p> <pre> * * * * * * </pre> <p>Now the value of i and j is increased to 3.</p> <p> <strong>Iteration 4:</strong> </p> <pre> For i=3, 3<4 (true) for j="3," 3<="3" < pre> <p>The first <strong>print</strong> statement prints four stars at the fourth row and the second <strong>println</strong> statement prints the spaces and throws the cursor at the next line.</p> <pre> * * * * * * * * * * </pre> <p>Now the value of i and j is increased to 4.</p> <pre> For i=4, 4<4 (false) < pre> <p>The execution of the program will terminate when the value of i will be equal to the number of rows.</p> <h2>Star Pattern</h2> <p> <strong>1. Right Triangle Star Pattern</strong> </p> <pre> public class RightTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row=6; //outer loop for rows for(i=0; i<row; i++) { inner loop for columns for(j="0;" j<="i;" j++) prints stars system.out.print(\\'* \\'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-2.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Left Triangle Star Pattern</strong> </p> <pre> public class LeftTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row = 6; //Outer loop work for rows for (i=0; i=0; j--) { //prints space between two stars System.out.print(' '); } //inner loop for columns for (j=0; j<=i; j++ ) { prints star system.out.print(\\'* \\'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-3.webp" alt="How to Print Pattern in Java"> <p> <strong>3. Pyramid Star Pattern</strong> </p> <pre> public class PyramidPattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row = 6; //Outer loop work for rows for (i=0; i1; j--) { //prints space between two stars System.out.print(' '); } //inner loop for columns for (j=0; j<=i; j++ ) { prints star system.out.print(\\'* \\'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-4.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Diamond Shape Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondPattern { public static void main(String args[]) { int row, i, j, space = 1; System.out.print('Enter the number of rows you want to print: '); Scanner sc = new Scanner(System.in); row = sc.nextInt(); space = row - 1; for (j = 1; j<= row; j++) { for (i="1;" i<="space;" i++) system.out.print(\\' \\'); } space--; i <="2" * j - 1; system.out.print(\\'*\\'); system.out.println(\\'\\'); space="1;" (j="1;" j<="row" space++; (row j) pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-5.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Downward Triangle Star Pattern</strong> </p> <pre> public class DownwardTrianglePattern { public static void main(String[] args) { int rows=7; //inner loop for (int i= rows-1; i>=0 ; i--) { //outer loop for (int j=0; j<=i; j++) { prints star and space system.out.print(\\'*\\' + \\' \\'); } throws the cursor in next line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-6.webp" alt="How to Print Pattern in Java"> <p> <strong>6. Mirrored Right Triangle Star Pattern</strong> </p> <pre> public class MirroredRightTrianglePattern { public static void main(String[] args) { int n=7; //inner loop for (int i= 0; i<= n; i++) { outer loop for (int j="1;" j<="n-i;" j++) system.out.print(\\' \\'); } k="0;k<=i;k++)" system.out.print(\\'*\\'); system.out.println(\\'\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-7.webp" alt="How to Print Pattern in Java"> <p> <strong>7. Reverse Pyramid Star Pattern</strong> </p> <pre> public class ReversePyramidPattern { public static void main(String[] args) { int rows=8; for (int i= 0; i<= rows-1; i++) { for (int j="0;" j<="i;" j++) system.out.print(\\' \\'); } k="0;" k<="rows-1-i;" k++) system.out.print(\\'*\\' + \\' system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-8.webp" alt="How to Print Pattern in Java"> <p> <strong>8. Right Down Mirror Star Pattern</strong> </p> <pre> public class RightDownMirrorPattern { public static void main(String args[]) { int row=7; for (int i= row; i>= 1; i--) { for (int j=row; j>i;j--) { System.out.print(' '); } for (int k=1;k<=i;k++) { system.out.print(\\'*\\'); } system.out.println(\\'\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-9.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Right Pascal's Triangle</strong> </p> <pre> import java.util.Scanner; public class RightPascalTrianglePattern { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i= 0; i<= rows-1; i++) { for (j="0;" j<="i;" j++) system.out.print(\\'*\\'+ \\' \\'); } system.out.println(\\'\\'); (i="rows-1;" i>=0; i--) { for(j=0; j <= i-1;j++) { system.out.print(\\'*\\'+ \\' \\'); } system.out.println(\\'\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-10.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Left Pascal's Triangle</strong> </p> <pre> import java.util.Scanner; public class LeftPascalTrianglePattern { public static void main(String[] args) { int i, j, k, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i= 1; i<= rows ; i++) { for (j="i;" j <rows ;j++) system.out.print(\\' \\'); } (k="1;" k<="i;k++)" system.out.print(\\'*\\'); system.out.println(\\'\\'); (i="rows;" i>=1; i--) { for(j=i; j<=rows;j++) { system.out.print(\\' \\'); } for(k="1;" k <i ;k++) system.out.print(\\'*\\'); system.out.println(\\'\\'); sc.close(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-11.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Sandglass Star Pattern</strong> </p> <pre> import java.util.Scanner; public class SandglassPattern { public static void main(String[] args) { int i, j, k, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i= 0; i<= n-1 ; i++) { for (j="0;" j <i; j++) system.out.print(\\' \\'); } (k="i;" k<="n-1;" k++) system.out.print(\\'*\\' + \\' system.out.println(\\'\\'); (i="n-1;" i>= 0; i--) { for (j=0; j <i; j++) { system.out.print(\\' \\'); } for (k="i;" k<="n-1;" k++) system.out.print(\\'*\\' + \\' system.out.println(\\'\\'); sc.close(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-12.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Alphabet Star Pattern</strong> </p> <pre> import java.util.*; public class AlphabetPattern { public static void main(String[] args) { int i, j, n=8; // Outer for loop for number of lines for (i = 0; i<=n; i++) { inner for loop logic execution (j="0;" j<="n" 2; j++) prints middle line i="=" n 2) system.out.print(\\'*\\'); else system.out.print(\\' \\'); system.out.println(); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-13.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Triangle Star Pattern</strong> </p> <pre> import java.util.Scanner; public class TrianglePattern { public static void main(String[] args) { int i, j, k, rows=9; for (i=1; i<= rows ; i++) { for (j="i;" j < j++) system.out.print(\\' \\'); } (k="1;" k -1) ;k++) if(k="=1" || i="=" system.out.print(\\'*\\'); else system.out.println(\\'\\'); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-14.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Down Triangle Pattern</strong> </p> <pre> import java.util.Scanner; public class DownTrianglePattern { public static void main(String[] args) { int i, j, k, rows=9; for (i=rows; i>= 1 ; i--) { for (j = i; j<rows ; j++) { system.out.print(\\' \\'); } for (k="1;" k <="(2*i" -1) ;k++) if( || i="=" rows system.out.print(\\'*\\'); else system.out.println(\\'\\'); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-15.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Diamond Star Pattern</strong> </p> <pre> import java.util.*; public class DiamondPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println('Enter the number of rows you want to print: '); int rows = sc.nextInt(); for (i=1; i i ; j--) { System.out.print(' '); } System.out.print('*'); for (k = 1; k <2*(i -1) ;k++) { system.out.print(\\' \\'); } if( i="=1)" system.out.println(\\'\\'); else system.out.println(\\'*\\'); for (i="rows-1;">= 1 ; i--) { for (int j = rows; j > i ; j--) { System.out.print(' '); } System.out.print('*'); for (int k = 1; k <2*(i -1) ;k++) { system.out.print(\\' \\'); } if(i="=1)" system.out.println(\\'\\'); else system.out.println(\\'*\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-16.webp" alt="How to Print Pattern in Java"> <h2>Number Pattern</h2> <p> <strong>1. Pattern-1</strong> </p> <pre> public class Pattern1 { public static void main(String args[]) { int i, j,number, n=7; //loop for rows for(i=0; i<n; i++) { number="1;" loop for columns for(j="0;" j<="i;" j++) prints num system.out.print(number+ \\' \\'); incrementing the value of number++; } throws cursor at next line after printing each row system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-17.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Pattern-2</strong> </p> <pre> public class Pattern2 { public static void main(String[] args) { int i, j, k = 1; //inner loop for (i = 1; i <= 7; i++) { outer loop for (j="1;" j< i + 1; j++) prints the value of k system.out.print(k++ \\' \\'); } throws cursor at next line system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-18.webp" alt="How to Print Pattern in Java"> <p> <strong>3. Pattern-3</strong> </p> <pre> public class Pattern3 { public static void main(String[] args) { int n = 8; //n is the number of rows you want to print for (int i = 0; i <n; 2 i++) { int number="1;" system.out.printf(\\'%\\' + (n - i) * \\'s\\', \\'\\'); for (int j="0;" <="i;" j++) system.out.printf(\\'%4d\\', number); (i j) (j 1); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-19.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Pattern-4</strong> </p> <pre> public class Pattern4 { public static void main(String[] args) { for (int i = 1; i <= 4; i++) { int n="8;" for (int j="1;" k--) system.out.print(k); } l="2;" <="i;" l++) system.out.print(l); system.out.println(); i="3;">= 1; i--) { int n = 10; for (int j = 0; j= 1; k--) { System.out.print(k); } for (int l = 2; l <= i; l++) { system.out.print(l); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-20.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Pattern-5</strong> </p> <pre> import java.util.*; public class Pattern5 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) system.out.print(i+\\' \\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-21.webp" alt="How to Print Pattern in Java"> <p> <strong>6. Pattern-6</strong> </p> <pre> import java.util.*; public class Pattern6 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows youy want to print: '); rows = sc.nextInt(); for (i = rows; i >= 1; i--) { for (j = rows; j >= i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-22.webp" alt="How to Print Pattern in Java"> <p> <strong>7. Pattern-7</strong> </p> <pre> import java.util.Scanner; public class Pattern7 { public static void main(String[] args) { int i, j, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-23.webp" alt="How to Print Pattern in Java"> <p> <strong>8. Pattern-8</strong> </p> <pre> public class Pattern8 { public static void main(String[] args) { int rows=9; //number of rows to print for (int i = 1; i <= 1 rows; i++) { int num; if(i%2="=" 0) num="0;" for (int j="1;" <="rows;" j++) system.out.print(num);="="0)?"" : 0; } else system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-24.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Pattern-9</strong> </p> <pre> import java.util.Scanner; public class Pattern9 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) if(j%2="=" 0) system.out.print(0); } else system.out.print(1); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-25.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Pattern-10</strong> </p> <pre> public class Pattern10 { public static void main(String[] args) { int n = 10; for (int i = 1; i <= n; i++) { for (int j="1;" < i; j++) system.out.print(\\' \\'); } k="i;" k++) system.out.print(k+\\' system.out.println(); i="n-1;">= 1; i--) { for (int j = 1; j <i; j++) { system.out.print(\\' \\'); } for (int k="i;" <="n;" k++) system.out.print(k+\\' system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-26.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Pattern-11</strong> </p> <pre> public class Pattern11 { public static void main(String[] args) { int rows=8; //Prints upper half pattern for (int i = 1; i <= rows; i++) { for (int j="1;" <="i;" j++) system.out.print(j+\\' \\'); } system.out.println(); prints lower half pattern i="rows-1;">= 1; i--) { for (int j = 1; j <= i; j++) { system.out.print(j+\\' \\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-27.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Pattern-12</strong> </p> <pre> public class Pattern12 { public static void main(String[] args) { int rows=9; for (int i = 1; i = i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-28.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Pattern-13</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int i, j, rows=9; for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\' \\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-29.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Pattern-14</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int rows=7, i, j; for (i = rows; i >= 1; i--) { for (j = i; j >= 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-30.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Pattern-15</strong> </p> <pre> public class Pattern15 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { prints first half of the row for (j="1;" j="1;" j--) system.out.print(j+\\' \\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-31.webp" alt="How to Print Pattern in Java"> <p> <strong>16. Pattern-16</strong> </p> <pre> public class Pattern16 { public static void main(String[] args) { int i, j, rows=9; //Prints upper half pattern for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\' \\'); } system.out.println(); prints lower half pattern for (i="2;" i <="rows;" i++) (j="1;" j pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-32.webp" alt="How to Print Pattern in Java"> <p> <strong>17. Pattern-17</strong> </p> <pre> public class Pattern17 { public static void main(String[] args) { int rows=9; //Prints upper half pattern for (int i = 1; i <= rows; i++) { prints i spaces at the beginning of each row for (int j="1;" < i; j++) system.out.print(\\' \\'); } to rows value end system.out.print(j); system.out.println(); lower half pattern>= 1; i--) { //Prints i spaces at the beginning of each row for (int j = 1; j <i; j++) { system.out.print(\\' \\'); } prints i to rows value at the end of each row for (int j="i;" <="rows;" system.out.print(j); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-33.webp" alt="How to Print Pattern in Java"> <p> <strong>18. Pattern-18</strong> </p> <pre> public class Pattern18 { public static void main(String[] args) { int rows=8; for (int i = 1; i <= rows; i++) { for (int j="1;" <="rows-i;" j++) system.out.print(1); } system.out.print(i); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-34.webp" alt="How to Print Pattern in Java"> <p> <strong>19. Pattern-19</strong> </p> <pre> public class Pattern19 { public static void main(String args[]) { int rows=9; for (int i = 1; i <= rows; i++) { int num="i;" for (int j="1;" <="i;" j++) system.out.print(num+\\' \\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-35.webp" alt="How to Print Pattern in Java"> <p> <strong>20. Pattern-20</strong> </p> <pre> public class Pattern20 { public static void main(String[] args) { int i, j, k, rows=9; for(i=1;i<rows+1 ;i++) { for(j="i;" j < rows+1 ;j++) system.out.print(j + \\' \\'); } for(k="1;" k i ;k++) system.out.print(k system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-36.webp" alt="How to Print Pattern in Java"> <p> <strong>21. Pattern-21</strong> </p> <pre> import java.util.Scanner; public class Pattern21 { public static void main(String[] args) { int i, j, min, n; //n is the number up to you want to print System.out.print('Enter the value of n: '); Scanner sc= new Scanner(System.in); n=sc.nextInt(); //loo for upper left part for (i = 1; i <= n; i++) { for (j="1;" j <="n;" j++) min="i" <j ? i : j; system.out.print(n - + 1+ \\' \\'); } loop upper right part 1;>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\' \\'); } system.out.println(); loop for lower left part (i="n" 1;>= 1; i--) { for (j = 1; j <= n; j++) { min="i" <j ? i : j; system.out.print(n - + 1+ \\' \\'); } loop for lower right part (j="n" 1; j>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\' \\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-37.webp" alt="How to Print Pattern in Java"> <h2>Character Pattern</h2> <p> <strong>1. Right Triangle Alphabetic Pattern</strong> </p> <pre> public class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 //inner loop for rows for (int i = 0; i <= 8; i++) { outer loop for columns (int j="0;" <="i;" j++) adds the value of in ascii a and prints corresponding alphabet system.out.print((char) (alphabet + j) \\' \\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-38.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Repeating Alphabet Pattern</strong> </p> <pre> public class RepeatingPattern { public static void main(String[] args) { int letter = 65; //ASCII value of capital A is 65 //inner loop for rwos for (int i = 0; i<= 9; i++) { outer loop for columns (int j="0;" <="i;" j++) prints the character system.out.print((char) letter + \\' \\'); } letter++; system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-39.webp" alt="How to Print Pattern in Java"> <p> <strong>3. K-shape Alphabet Pattern</strong> </p> <pre> public class KshapePattern { public static void main(String[] args) { for (int i = 8; i >= 0; i--) { int alphabet = 65; for (int j = 0; j <= i; j++) { system.out.print((char) (alphabet + j) \\' \\'); } system.out.println(); for (int i="0;" i<="8;" i++) int alphabet="65;" j="0;" <="i;" pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-40.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Triangle Character Pattern</strong> </p> <pre> public class TriangleCharacterPattern { public static void main(String[] args) { for (int i = 0; i i; j--) { System.out.print(' '); } for (int k = 0; k <= i; k++) { system.out.print((char) (alphabet + k) \\' \\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-41.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Diamond Character Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondCharacterPattern { public static void main(String[] args) { char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print('Enter a Character between A to Z : '); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next('[A-Z]').charAt(0); // search for letter number in the array letter for (int i = 0; i <alphabet.length; i++) { if (letter[i]="=" user_ alphabet) alphabet _number="i;" break; } construct diamond for (int i="0;" <="alphabet" _number; diamond[i] ; add initial spaces j="0;" - i; j++) +=" " space between letters (alphabet[i] !="A" ) <2 * 1; draw the first part of system.out.println(diamond[i]);>= 0; i--) { // Draw the second part of the diamond // prints the diamondArray in the reverse order System.out.println(diamond[i]); } } catch (Exception e) { e.printStackTrace(); } finally { reader.close(); } } } </alphabet.length;></pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-42.webp" alt="How to Print Pattern in Java"> <hr></=></pre></=></pre></=></pre></=></pre></j></=></j></=></pre></rows+1></pre></=></pre></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></n;></pre></=></pre></n;></pre></2*(i></2*(i></pre></rows></pre></=></pre></=n;></pre></i;></=></pre></=rows;j++)></=></pre></=></=></pre></=i;k++)></pre></=></pre></=></pre></=i;></pre></=></pre></=i;></pre></=i;></pre></row;></pre></4></pre></4></pre></4>
Nu er værdien af i og j øget til 3.
Gentagelse 4:
For i=3, 3<4 (true) for j="3," 3<="3" < pre> <p>The first <strong>print</strong> statement prints four stars at the fourth row and the second <strong>println</strong> statement prints the spaces and throws the cursor at the next line.</p> <pre> * * * * * * * * * * </pre> <p>Now the value of i and j is increased to 4.</p> <pre> For i=4, 4<4 (false) < pre> <p>The execution of the program will terminate when the value of i will be equal to the number of rows.</p> <h2>Star Pattern</h2> <p> <strong>1. Right Triangle Star Pattern</strong> </p> <pre> public class RightTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row=6; //outer loop for rows for(i=0; i<row; i++) { inner loop for columns for(j="0;" j<="i;" j++) prints stars system.out.print(\\'* \\'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-2.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Left Triangle Star Pattern</strong> </p> <pre> public class LeftTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row = 6; //Outer loop work for rows for (i=0; i=0; j--) { //prints space between two stars System.out.print(' '); } //inner loop for columns for (j=0; j<=i; j++ ) { prints star system.out.print(\\'* \\'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-3.webp" alt="How to Print Pattern in Java"> <p> <strong>3. Pyramid Star Pattern</strong> </p> <pre> public class PyramidPattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row = 6; //Outer loop work for rows for (i=0; i1; j--) { //prints space between two stars System.out.print(' '); } //inner loop for columns for (j=0; j<=i; j++ ) { prints star system.out.print(\\'* \\'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-4.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Diamond Shape Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondPattern { public static void main(String args[]) { int row, i, j, space = 1; System.out.print('Enter the number of rows you want to print: '); Scanner sc = new Scanner(System.in); row = sc.nextInt(); space = row - 1; for (j = 1; j<= row; j++) { for (i="1;" i<="space;" i++) system.out.print(\\' \\'); } space--; i <="2" * j - 1; system.out.print(\\'*\\'); system.out.println(\\'\\'); space="1;" (j="1;" j<="row" space++; (row j) pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-5.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Downward Triangle Star Pattern</strong> </p> <pre> public class DownwardTrianglePattern { public static void main(String[] args) { int rows=7; //inner loop for (int i= rows-1; i>=0 ; i--) { //outer loop for (int j=0; j<=i; j++) { prints star and space system.out.print(\\'*\\' + \\' \\'); } throws the cursor in next line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-6.webp" alt="How to Print Pattern in Java"> <p> <strong>6. Mirrored Right Triangle Star Pattern</strong> </p> <pre> public class MirroredRightTrianglePattern { public static void main(String[] args) { int n=7; //inner loop for (int i= 0; i<= n; i++) { outer loop for (int j="1;" j<="n-i;" j++) system.out.print(\\' \\'); } k="0;k<=i;k++)" system.out.print(\\'*\\'); system.out.println(\\'\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-7.webp" alt="How to Print Pattern in Java"> <p> <strong>7. Reverse Pyramid Star Pattern</strong> </p> <pre> public class ReversePyramidPattern { public static void main(String[] args) { int rows=8; for (int i= 0; i<= rows-1; i++) { for (int j="0;" j<="i;" j++) system.out.print(\\' \\'); } k="0;" k<="rows-1-i;" k++) system.out.print(\\'*\\' + \\' system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-8.webp" alt="How to Print Pattern in Java"> <p> <strong>8. Right Down Mirror Star Pattern</strong> </p> <pre> public class RightDownMirrorPattern { public static void main(String args[]) { int row=7; for (int i= row; i>= 1; i--) { for (int j=row; j>i;j--) { System.out.print(' '); } for (int k=1;k<=i;k++) { system.out.print(\\'*\\'); } system.out.println(\\'\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-9.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Right Pascal's Triangle</strong> </p> <pre> import java.util.Scanner; public class RightPascalTrianglePattern { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i= 0; i<= rows-1; i++) { for (j="0;" j<="i;" j++) system.out.print(\\'*\\'+ \\' \\'); } system.out.println(\\'\\'); (i="rows-1;" i>=0; i--) { for(j=0; j <= i-1;j++) { system.out.print(\\'*\\'+ \\' \\'); } system.out.println(\\'\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-10.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Left Pascal's Triangle</strong> </p> <pre> import java.util.Scanner; public class LeftPascalTrianglePattern { public static void main(String[] args) { int i, j, k, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i= 1; i<= rows ; i++) { for (j="i;" j <rows ;j++) system.out.print(\\' \\'); } (k="1;" k<="i;k++)" system.out.print(\\'*\\'); system.out.println(\\'\\'); (i="rows;" i>=1; i--) { for(j=i; j<=rows;j++) { system.out.print(\\' \\'); } for(k="1;" k <i ;k++) system.out.print(\\'*\\'); system.out.println(\\'\\'); sc.close(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-11.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Sandglass Star Pattern</strong> </p> <pre> import java.util.Scanner; public class SandglassPattern { public static void main(String[] args) { int i, j, k, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i= 0; i<= n-1 ; i++) { for (j="0;" j <i; j++) system.out.print(\\' \\'); } (k="i;" k<="n-1;" k++) system.out.print(\\'*\\' + \\' system.out.println(\\'\\'); (i="n-1;" i>= 0; i--) { for (j=0; j <i; j++) { system.out.print(\\' \\'); } for (k="i;" k<="n-1;" k++) system.out.print(\\'*\\' + \\' system.out.println(\\'\\'); sc.close(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-12.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Alphabet Star Pattern</strong> </p> <pre> import java.util.*; public class AlphabetPattern { public static void main(String[] args) { int i, j, n=8; // Outer for loop for number of lines for (i = 0; i<=n; i++) { inner for loop logic execution (j="0;" j<="n" 2; j++) prints middle line i="=" n 2) system.out.print(\\'*\\'); else system.out.print(\\' \\'); system.out.println(); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-13.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Triangle Star Pattern</strong> </p> <pre> import java.util.Scanner; public class TrianglePattern { public static void main(String[] args) { int i, j, k, rows=9; for (i=1; i<= rows ; i++) { for (j="i;" j < j++) system.out.print(\\' \\'); } (k="1;" k -1) ;k++) if(k="=1" || i="=" system.out.print(\\'*\\'); else system.out.println(\\'\\'); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-14.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Down Triangle Pattern</strong> </p> <pre> import java.util.Scanner; public class DownTrianglePattern { public static void main(String[] args) { int i, j, k, rows=9; for (i=rows; i>= 1 ; i--) { for (j = i; j<rows ; j++) { system.out.print(\\' \\'); } for (k="1;" k <="(2*i" -1) ;k++) if( || i="=" rows system.out.print(\\'*\\'); else system.out.println(\\'\\'); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-15.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Diamond Star Pattern</strong> </p> <pre> import java.util.*; public class DiamondPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println('Enter the number of rows you want to print: '); int rows = sc.nextInt(); for (i=1; i i ; j--) { System.out.print(' '); } System.out.print('*'); for (k = 1; k <2*(i -1) ;k++) { system.out.print(\\' \\'); } if( i="=1)" system.out.println(\\'\\'); else system.out.println(\\'*\\'); for (i="rows-1;">= 1 ; i--) { for (int j = rows; j > i ; j--) { System.out.print(' '); } System.out.print('*'); for (int k = 1; k <2*(i -1) ;k++) { system.out.print(\\' \\'); } if(i="=1)" system.out.println(\\'\\'); else system.out.println(\\'*\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-16.webp" alt="How to Print Pattern in Java"> <h2>Number Pattern</h2> <p> <strong>1. Pattern-1</strong> </p> <pre> public class Pattern1 { public static void main(String args[]) { int i, j,number, n=7; //loop for rows for(i=0; i<n; i++) { number="1;" loop for columns for(j="0;" j<="i;" j++) prints num system.out.print(number+ \\' \\'); incrementing the value of number++; } throws cursor at next line after printing each row system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-17.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Pattern-2</strong> </p> <pre> public class Pattern2 { public static void main(String[] args) { int i, j, k = 1; //inner loop for (i = 1; i <= 7; i++) { outer loop for (j="1;" j< i + 1; j++) prints the value of k system.out.print(k++ \\' \\'); } throws cursor at next line system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-18.webp" alt="How to Print Pattern in Java"> <p> <strong>3. Pattern-3</strong> </p> <pre> public class Pattern3 { public static void main(String[] args) { int n = 8; //n is the number of rows you want to print for (int i = 0; i <n; 2 i++) { int number="1;" system.out.printf(\\'%\\' + (n - i) * \\'s\\', \\'\\'); for (int j="0;" <="i;" j++) system.out.printf(\\'%4d\\', number); (i j) (j 1); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-19.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Pattern-4</strong> </p> <pre> public class Pattern4 { public static void main(String[] args) { for (int i = 1; i <= 4; i++) { int n="8;" for (int j="1;" k--) system.out.print(k); } l="2;" <="i;" l++) system.out.print(l); system.out.println(); i="3;">= 1; i--) { int n = 10; for (int j = 0; j= 1; k--) { System.out.print(k); } for (int l = 2; l <= i; l++) { system.out.print(l); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-20.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Pattern-5</strong> </p> <pre> import java.util.*; public class Pattern5 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) system.out.print(i+\\' \\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-21.webp" alt="How to Print Pattern in Java"> <p> <strong>6. Pattern-6</strong> </p> <pre> import java.util.*; public class Pattern6 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows youy want to print: '); rows = sc.nextInt(); for (i = rows; i >= 1; i--) { for (j = rows; j >= i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-22.webp" alt="How to Print Pattern in Java"> <p> <strong>7. Pattern-7</strong> </p> <pre> import java.util.Scanner; public class Pattern7 { public static void main(String[] args) { int i, j, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-23.webp" alt="How to Print Pattern in Java"> <p> <strong>8. Pattern-8</strong> </p> <pre> public class Pattern8 { public static void main(String[] args) { int rows=9; //number of rows to print for (int i = 1; i <= 1 rows; i++) { int num; if(i%2="=" 0) num="0;" for (int j="1;" <="rows;" j++) system.out.print(num);="="0)?"" : 0; } else system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-24.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Pattern-9</strong> </p> <pre> import java.util.Scanner; public class Pattern9 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) if(j%2="=" 0) system.out.print(0); } else system.out.print(1); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-25.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Pattern-10</strong> </p> <pre> public class Pattern10 { public static void main(String[] args) { int n = 10; for (int i = 1; i <= n; i++) { for (int j="1;" < i; j++) system.out.print(\\' \\'); } k="i;" k++) system.out.print(k+\\' system.out.println(); i="n-1;">= 1; i--) { for (int j = 1; j <i; j++) { system.out.print(\\' \\'); } for (int k="i;" <="n;" k++) system.out.print(k+\\' system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-26.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Pattern-11</strong> </p> <pre> public class Pattern11 { public static void main(String[] args) { int rows=8; //Prints upper half pattern for (int i = 1; i <= rows; i++) { for (int j="1;" <="i;" j++) system.out.print(j+\\' \\'); } system.out.println(); prints lower half pattern i="rows-1;">= 1; i--) { for (int j = 1; j <= i; j++) { system.out.print(j+\\' \\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-27.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Pattern-12</strong> </p> <pre> public class Pattern12 { public static void main(String[] args) { int rows=9; for (int i = 1; i = i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-28.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Pattern-13</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int i, j, rows=9; for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\' \\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-29.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Pattern-14</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int rows=7, i, j; for (i = rows; i >= 1; i--) { for (j = i; j >= 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-30.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Pattern-15</strong> </p> <pre> public class Pattern15 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { prints first half of the row for (j="1;" j="1;" j--) system.out.print(j+\\' \\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-31.webp" alt="How to Print Pattern in Java"> <p> <strong>16. Pattern-16</strong> </p> <pre> public class Pattern16 { public static void main(String[] args) { int i, j, rows=9; //Prints upper half pattern for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\' \\'); } system.out.println(); prints lower half pattern for (i="2;" i <="rows;" i++) (j="1;" j pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-32.webp" alt="How to Print Pattern in Java"> <p> <strong>17. Pattern-17</strong> </p> <pre> public class Pattern17 { public static void main(String[] args) { int rows=9; //Prints upper half pattern for (int i = 1; i <= rows; i++) { prints i spaces at the beginning of each row for (int j="1;" < i; j++) system.out.print(\\' \\'); } to rows value end system.out.print(j); system.out.println(); lower half pattern>= 1; i--) { //Prints i spaces at the beginning of each row for (int j = 1; j <i; j++) { system.out.print(\\' \\'); } prints i to rows value at the end of each row for (int j="i;" <="rows;" system.out.print(j); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-33.webp" alt="How to Print Pattern in Java"> <p> <strong>18. Pattern-18</strong> </p> <pre> public class Pattern18 { public static void main(String[] args) { int rows=8; for (int i = 1; i <= rows; i++) { for (int j="1;" <="rows-i;" j++) system.out.print(1); } system.out.print(i); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-34.webp" alt="How to Print Pattern in Java"> <p> <strong>19. Pattern-19</strong> </p> <pre> public class Pattern19 { public static void main(String args[]) { int rows=9; for (int i = 1; i <= rows; i++) { int num="i;" for (int j="1;" <="i;" j++) system.out.print(num+\\' \\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-35.webp" alt="How to Print Pattern in Java"> <p> <strong>20. Pattern-20</strong> </p> <pre> public class Pattern20 { public static void main(String[] args) { int i, j, k, rows=9; for(i=1;i<rows+1 ;i++) { for(j="i;" j < rows+1 ;j++) system.out.print(j + \\' \\'); } for(k="1;" k i ;k++) system.out.print(k system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-36.webp" alt="How to Print Pattern in Java"> <p> <strong>21. Pattern-21</strong> </p> <pre> import java.util.Scanner; public class Pattern21 { public static void main(String[] args) { int i, j, min, n; //n is the number up to you want to print System.out.print('Enter the value of n: '); Scanner sc= new Scanner(System.in); n=sc.nextInt(); //loo for upper left part for (i = 1; i <= n; i++) { for (j="1;" j <="n;" j++) min="i" <j ? i : j; system.out.print(n - + 1+ \\' \\'); } loop upper right part 1;>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\' \\'); } system.out.println(); loop for lower left part (i="n" 1;>= 1; i--) { for (j = 1; j <= n; j++) { min="i" <j ? i : j; system.out.print(n - + 1+ \\' \\'); } loop for lower right part (j="n" 1; j>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\' \\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-37.webp" alt="How to Print Pattern in Java"> <h2>Character Pattern</h2> <p> <strong>1. Right Triangle Alphabetic Pattern</strong> </p> <pre> public class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 //inner loop for rows for (int i = 0; i <= 8; i++) { outer loop for columns (int j="0;" <="i;" j++) adds the value of in ascii a and prints corresponding alphabet system.out.print((char) (alphabet + j) \\' \\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-38.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Repeating Alphabet Pattern</strong> </p> <pre> public class RepeatingPattern { public static void main(String[] args) { int letter = 65; //ASCII value of capital A is 65 //inner loop for rwos for (int i = 0; i<= 9; i++) { outer loop for columns (int j="0;" <="i;" j++) prints the character system.out.print((char) letter + \\' \\'); } letter++; system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-39.webp" alt="How to Print Pattern in Java"> <p> <strong>3. K-shape Alphabet Pattern</strong> </p> <pre> public class KshapePattern { public static void main(String[] args) { for (int i = 8; i >= 0; i--) { int alphabet = 65; for (int j = 0; j <= i; j++) { system.out.print((char) (alphabet + j) \\' \\'); } system.out.println(); for (int i="0;" i<="8;" i++) int alphabet="65;" j="0;" <="i;" pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-40.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Triangle Character Pattern</strong> </p> <pre> public class TriangleCharacterPattern { public static void main(String[] args) { for (int i = 0; i i; j--) { System.out.print(' '); } for (int k = 0; k <= i; k++) { system.out.print((char) (alphabet + k) \\' \\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-41.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Diamond Character Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondCharacterPattern { public static void main(String[] args) { char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print('Enter a Character between A to Z : '); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next('[A-Z]').charAt(0); // search for letter number in the array letter for (int i = 0; i <alphabet.length; i++) { if (letter[i]="=" user_ alphabet) alphabet _number="i;" break; } construct diamond for (int i="0;" <="alphabet" _number; diamond[i] ; add initial spaces j="0;" - i; j++) +=" " space between letters (alphabet[i] !="A" ) <2 * 1; draw the first part of system.out.println(diamond[i]);>= 0; i--) { // Draw the second part of the diamond // prints the diamondArray in the reverse order System.out.println(diamond[i]); } } catch (Exception e) { e.printStackTrace(); } finally { reader.close(); } } } </alphabet.length;></pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-42.webp" alt="How to Print Pattern in Java"> <hr></=></pre></=></pre></=></pre></=></pre></j></=></j></=></pre></rows+1></pre></=></pre></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></n;></pre></=></pre></n;></pre></2*(i></2*(i></pre></rows></pre></=></pre></=n;></pre></i;></=></pre></=rows;j++)></=></pre></=></=></pre></=i;k++)></pre></=></pre></=></pre></=i;></pre></=></pre></=i;></pre></=i;></pre></row;></pre></4></pre></4>
Nu er værdien af i og j øget til 4.
For i=4, 4<4 (false) < pre> <p>The execution of the program will terminate when the value of i will be equal to the number of rows.</p> <h2>Star Pattern</h2> <p> <strong>1. Right Triangle Star Pattern</strong> </p> <pre> public class RightTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row=6; //outer loop for rows for(i=0; i<row; i++) { inner loop for columns for(j="0;" j<="i;" j++) prints stars system.out.print(\\\'* \\\'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-2.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Left Triangle Star Pattern</strong> </p> <pre> public class LeftTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row = 6; //Outer loop work for rows for (i=0; i=0; j--) { //prints space between two stars System.out.print(' '); } //inner loop for columns for (j=0; j<=i; j++ ) { prints star system.out.print(\\\'* \\\'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-3.webp" alt="How to Print Pattern in Java"> <p> <strong>3. Pyramid Star Pattern</strong> </p> <pre> public class PyramidPattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row = 6; //Outer loop work for rows for (i=0; i1; j--) { //prints space between two stars System.out.print(' '); } //inner loop for columns for (j=0; j<=i; j++ ) { prints star system.out.print(\\\'* \\\'); } throws the cursor in a new line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-4.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Diamond Shape Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondPattern { public static void main(String args[]) { int row, i, j, space = 1; System.out.print('Enter the number of rows you want to print: '); Scanner sc = new Scanner(System.in); row = sc.nextInt(); space = row - 1; for (j = 1; j<= row; j++) { for (i="1;" i<="space;" i++) system.out.print(\\\' \\\'); } space--; i <="2" * j - 1; system.out.print(\\\'*\\\'); system.out.println(\\\'\\\'); space="1;" (j="1;" j<="row" space++; (row j) pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-5.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Downward Triangle Star Pattern</strong> </p> <pre> public class DownwardTrianglePattern { public static void main(String[] args) { int rows=7; //inner loop for (int i= rows-1; i>=0 ; i--) { //outer loop for (int j=0; j<=i; j++) { prints star and space system.out.print(\\\'*\\\' + \\\' \\\'); } throws the cursor in next line after printing each system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-6.webp" alt="How to Print Pattern in Java"> <p> <strong>6. Mirrored Right Triangle Star Pattern</strong> </p> <pre> public class MirroredRightTrianglePattern { public static void main(String[] args) { int n=7; //inner loop for (int i= 0; i<= n; i++) { outer loop for (int j="1;" j<="n-i;" j++) system.out.print(\\\' \\\'); } k="0;k<=i;k++)" system.out.print(\\\'*\\\'); system.out.println(\\\'\\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-7.webp" alt="How to Print Pattern in Java"> <p> <strong>7. Reverse Pyramid Star Pattern</strong> </p> <pre> public class ReversePyramidPattern { public static void main(String[] args) { int rows=8; for (int i= 0; i<= rows-1; i++) { for (int j="0;" j<="i;" j++) system.out.print(\\\' \\\'); } k="0;" k<="rows-1-i;" k++) system.out.print(\\\'*\\\' + \\\' system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-8.webp" alt="How to Print Pattern in Java"> <p> <strong>8. Right Down Mirror Star Pattern</strong> </p> <pre> public class RightDownMirrorPattern { public static void main(String args[]) { int row=7; for (int i= row; i>= 1; i--) { for (int j=row; j>i;j--) { System.out.print(' '); } for (int k=1;k<=i;k++) { system.out.print(\\\'*\\\'); } system.out.println(\\\'\\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-9.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Right Pascal's Triangle</strong> </p> <pre> import java.util.Scanner; public class RightPascalTrianglePattern { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i= 0; i<= rows-1; i++) { for (j="0;" j<="i;" j++) system.out.print(\\\'*\\\'+ \\\' \\\'); } system.out.println(\\\'\\\'); (i="rows-1;" i>=0; i--) { for(j=0; j <= i-1;j++) { system.out.print(\\\'*\\\'+ \\\' \\\'); } system.out.println(\\\'\\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-10.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Left Pascal's Triangle</strong> </p> <pre> import java.util.Scanner; public class LeftPascalTrianglePattern { public static void main(String[] args) { int i, j, k, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i= 1; i<= rows ; i++) { for (j="i;" j <rows ;j++) system.out.print(\\\' \\\'); } (k="1;" k<="i;k++)" system.out.print(\\\'*\\\'); system.out.println(\\\'\\\'); (i="rows;" i>=1; i--) { for(j=i; j<=rows;j++) { system.out.print(\\\' \\\'); } for(k="1;" k <i ;k++) system.out.print(\\\'*\\\'); system.out.println(\\\'\\\'); sc.close(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-11.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Sandglass Star Pattern</strong> </p> <pre> import java.util.Scanner; public class SandglassPattern { public static void main(String[] args) { int i, j, k, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i= 0; i<= n-1 ; i++) { for (j="0;" j <i; j++) system.out.print(\\\' \\\'); } (k="i;" k<="n-1;" k++) system.out.print(\\\'*\\\' + \\\' system.out.println(\\\'\\\'); (i="n-1;" i>= 0; i--) { for (j=0; j <i; j++) { system.out.print(\\\' \\\'); } for (k="i;" k<="n-1;" k++) system.out.print(\\\'*\\\' + \\\' system.out.println(\\\'\\\'); sc.close(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-12.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Alphabet Star Pattern</strong> </p> <pre> import java.util.*; public class AlphabetPattern { public static void main(String[] args) { int i, j, n=8; // Outer for loop for number of lines for (i = 0; i<=n; i++) { inner for loop logic execution (j="0;" j<="n" 2; j++) prints middle line i="=" n 2) system.out.print(\\\'*\\\'); else system.out.print(\\\' \\\'); system.out.println(); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-13.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Triangle Star Pattern</strong> </p> <pre> import java.util.Scanner; public class TrianglePattern { public static void main(String[] args) { int i, j, k, rows=9; for (i=1; i<= rows ; i++) { for (j="i;" j < j++) system.out.print(\\\' \\\'); } (k="1;" k -1) ;k++) if(k="=1" || i="=" system.out.print(\\\'*\\\'); else system.out.println(\\\'\\\'); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-14.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Down Triangle Pattern</strong> </p> <pre> import java.util.Scanner; public class DownTrianglePattern { public static void main(String[] args) { int i, j, k, rows=9; for (i=rows; i>= 1 ; i--) { for (j = i; j<rows ; j++) { system.out.print(\\\' \\\'); } for (k="1;" k <="(2*i" -1) ;k++) if( || i="=" rows system.out.print(\\\'*\\\'); else system.out.println(\\\'\\\'); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-15.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Diamond Star Pattern</strong> </p> <pre> import java.util.*; public class DiamondPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println('Enter the number of rows you want to print: '); int rows = sc.nextInt(); for (i=1; i i ; j--) { System.out.print(' '); } System.out.print('*'); for (k = 1; k <2*(i -1) ;k++) { system.out.print(\\\' \\\'); } if( i="=1)" system.out.println(\\\'\\\'); else system.out.println(\\\'*\\\'); for (i="rows-1;">= 1 ; i--) { for (int j = rows; j > i ; j--) { System.out.print(' '); } System.out.print('*'); for (int k = 1; k <2*(i -1) ;k++) { system.out.print(\\\' \\\'); } if(i="=1)" system.out.println(\\\'\\\'); else system.out.println(\\\'*\\\'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-16.webp" alt="How to Print Pattern in Java"> <h2>Number Pattern</h2> <p> <strong>1. Pattern-1</strong> </p> <pre> public class Pattern1 { public static void main(String args[]) { int i, j,number, n=7; //loop for rows for(i=0; i<n; i++) { number="1;" loop for columns for(j="0;" j<="i;" j++) prints num system.out.print(number+ \\\' \\\'); incrementing the value of number++; } throws cursor at next line after printing each row system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-17.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Pattern-2</strong> </p> <pre> public class Pattern2 { public static void main(String[] args) { int i, j, k = 1; //inner loop for (i = 1; i <= 7; i++) { outer loop for (j="1;" j< i + 1; j++) prints the value of k system.out.print(k++ \\\' \\\'); } throws cursor at next line system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-18.webp" alt="How to Print Pattern in Java"> <p> <strong>3. Pattern-3</strong> </p> <pre> public class Pattern3 { public static void main(String[] args) { int n = 8; //n is the number of rows you want to print for (int i = 0; i <n; 2 i++) { int number="1;" system.out.printf(\\\'%\\\' + (n - i) * \\\'s\\\', \\\'\\\'); for (int j="0;" <="i;" j++) system.out.printf(\\\'%4d\\\', number); (i j) (j 1); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-19.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Pattern-4</strong> </p> <pre> public class Pattern4 { public static void main(String[] args) { for (int i = 1; i <= 4; i++) { int n="8;" for (int j="1;" k--) system.out.print(k); } l="2;" <="i;" l++) system.out.print(l); system.out.println(); i="3;">= 1; i--) { int n = 10; for (int j = 0; j= 1; k--) { System.out.print(k); } for (int l = 2; l <= i; l++) { system.out.print(l); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-20.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Pattern-5</strong> </p> <pre> import java.util.*; public class Pattern5 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); rows = sc.nextInt(); for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) system.out.print(i+\\\' \\\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-21.webp" alt="How to Print Pattern in Java"> <p> <strong>6. Pattern-6</strong> </p> <pre> import java.util.*; public class Pattern6 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows youy want to print: '); rows = sc.nextInt(); for (i = rows; i >= 1; i--) { for (j = rows; j >= i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-22.webp" alt="How to Print Pattern in Java"> <p> <strong>7. Pattern-7</strong> </p> <pre> import java.util.Scanner; public class Pattern7 { public static void main(String[] args) { int i, j, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-23.webp" alt="How to Print Pattern in Java"> <p> <strong>8. Pattern-8</strong> </p> <pre> public class Pattern8 { public static void main(String[] args) { int rows=9; //number of rows to print for (int i = 1; i <= 1 rows; i++) { int num; if(i%2="=" 0) num="0;" for (int j="1;" <="rows;" j++) system.out.print(num);="="0)?"" : 0; } else system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-24.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Pattern-9</strong> </p> <pre> import java.util.Scanner; public class Pattern9 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) if(j%2="=" 0) system.out.print(0); } else system.out.print(1); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-25.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Pattern-10</strong> </p> <pre> public class Pattern10 { public static void main(String[] args) { int n = 10; for (int i = 1; i <= n; i++) { for (int j="1;" < i; j++) system.out.print(\\\' \\\'); } k="i;" k++) system.out.print(k+\\\' system.out.println(); i="n-1;">= 1; i--) { for (int j = 1; j <i; j++) { system.out.print(\\\' \\\'); } for (int k="i;" <="n;" k++) system.out.print(k+\\\' system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-26.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Pattern-11</strong> </p> <pre> public class Pattern11 { public static void main(String[] args) { int rows=8; //Prints upper half pattern for (int i = 1; i <= rows; i++) { for (int j="1;" <="i;" j++) system.out.print(j+\\\' \\\'); } system.out.println(); prints lower half pattern i="rows-1;">= 1; i--) { for (int j = 1; j <= i; j++) { system.out.print(j+\\\' \\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-27.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Pattern-12</strong> </p> <pre> public class Pattern12 { public static void main(String[] args) { int rows=9; for (int i = 1; i = i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-28.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Pattern-13</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int i, j, rows=9; for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\\' \\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-29.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Pattern-14</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int rows=7, i, j; for (i = rows; i >= 1; i--) { for (j = i; j >= 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-30.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Pattern-15</strong> </p> <pre> public class Pattern15 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { prints first half of the row for (j="1;" j="1;" j--) system.out.print(j+\\\' \\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-31.webp" alt="How to Print Pattern in Java"> <p> <strong>16. Pattern-16</strong> </p> <pre> public class Pattern16 { public static void main(String[] args) { int i, j, rows=9; //Prints upper half pattern for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\\' \\\'); } system.out.println(); prints lower half pattern for (i="2;" i <="rows;" i++) (j="1;" j pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-32.webp" alt="How to Print Pattern in Java"> <p> <strong>17. Pattern-17</strong> </p> <pre> public class Pattern17 { public static void main(String[] args) { int rows=9; //Prints upper half pattern for (int i = 1; i <= rows; i++) { prints i spaces at the beginning of each row for (int j="1;" < i; j++) system.out.print(\\\' \\\'); } to rows value end system.out.print(j); system.out.println(); lower half pattern>= 1; i--) { //Prints i spaces at the beginning of each row for (int j = 1; j <i; j++) { system.out.print(\\\' \\\'); } prints i to rows value at the end of each row for (int j="i;" <="rows;" system.out.print(j); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-33.webp" alt="How to Print Pattern in Java"> <p> <strong>18. Pattern-18</strong> </p> <pre> public class Pattern18 { public static void main(String[] args) { int rows=8; for (int i = 1; i <= rows; i++) { for (int j="1;" <="rows-i;" j++) system.out.print(1); } system.out.print(i); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-34.webp" alt="How to Print Pattern in Java"> <p> <strong>19. Pattern-19</strong> </p> <pre> public class Pattern19 { public static void main(String args[]) { int rows=9; for (int i = 1; i <= rows; i++) { int num="i;" for (int j="1;" <="i;" j++) system.out.print(num+\\\' \\\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-35.webp" alt="How to Print Pattern in Java"> <p> <strong>20. Pattern-20</strong> </p> <pre> public class Pattern20 { public static void main(String[] args) { int i, j, k, rows=9; for(i=1;i<rows+1 ;i++) { for(j="i;" j < rows+1 ;j++) system.out.print(j + \\\' \\\'); } for(k="1;" k i ;k++) system.out.print(k system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-36.webp" alt="How to Print Pattern in Java"> <p> <strong>21. Pattern-21</strong> </p> <pre> import java.util.Scanner; public class Pattern21 { public static void main(String[] args) { int i, j, min, n; //n is the number up to you want to print System.out.print('Enter the value of n: '); Scanner sc= new Scanner(System.in); n=sc.nextInt(); //loo for upper left part for (i = 1; i <= n; i++) { for (j="1;" j <="n;" j++) min="i" <j ? i : j; system.out.print(n - + 1+ \\\' \\\'); } loop upper right part 1;>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\\' \\\'); } system.out.println(); loop for lower left part (i="n" 1;>= 1; i--) { for (j = 1; j <= n; j++) { min="i" <j ? i : j; system.out.print(n - + 1+ \\\' \\\'); } loop for lower right part (j="n" 1; j>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\\' \\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-37.webp" alt="How to Print Pattern in Java"> <h2>Character Pattern</h2> <p> <strong>1. Right Triangle Alphabetic Pattern</strong> </p> <pre> public class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 //inner loop for rows for (int i = 0; i <= 8; i++) { outer loop for columns (int j="0;" <="i;" j++) adds the value of in ascii a and prints corresponding alphabet system.out.print((char) (alphabet + j) \\\' \\\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-38.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Repeating Alphabet Pattern</strong> </p> <pre> public class RepeatingPattern { public static void main(String[] args) { int letter = 65; //ASCII value of capital A is 65 //inner loop for rwos for (int i = 0; i<= 9; i++) { outer loop for columns (int j="0;" <="i;" j++) prints the character system.out.print((char) letter + \\\' \\\'); } letter++; system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-39.webp" alt="How to Print Pattern in Java"> <p> <strong>3. K-shape Alphabet Pattern</strong> </p> <pre> public class KshapePattern { public static void main(String[] args) { for (int i = 8; i >= 0; i--) { int alphabet = 65; for (int j = 0; j <= i; j++) { system.out.print((char) (alphabet + j) \\\' \\\'); } system.out.println(); for (int i="0;" i<="8;" i++) int alphabet="65;" j="0;" <="i;" pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-40.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Triangle Character Pattern</strong> </p> <pre> public class TriangleCharacterPattern { public static void main(String[] args) { for (int i = 0; i i; j--) { System.out.print(' '); } for (int k = 0; k <= i; k++) { system.out.print((char) (alphabet + k) \\\' \\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-41.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Diamond Character Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondCharacterPattern { public static void main(String[] args) { char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print('Enter a Character between A to Z : '); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next('[A-Z]').charAt(0); // search for letter number in the array letter for (int i = 0; i <alphabet.length; i++) { if (letter[i]="=" user_ alphabet) alphabet _number="i;" break; } construct diamond for (int i="0;" <="alphabet" _number; diamond[i] ; add initial spaces j="0;" - i; j++) +=" " space between letters (alphabet[i] !="A" ) <2 * 1; draw the first part of system.out.println(diamond[i]);>= 0; i--) { // Draw the second part of the diamond // prints the diamondArray in the reverse order System.out.println(diamond[i]); } } catch (Exception e) { e.printStackTrace(); } finally { reader.close(); } } } </alphabet.length;></pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-42.webp" alt="How to Print Pattern in Java"> <hr></=></pre></=></pre></=></pre></=></pre></j></=></j></=></pre></rows+1></pre></=></pre></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></n;></pre></=></pre></n;></pre></2*(i></2*(i></pre></rows></pre></=></pre></=n;></pre></i;></=></pre></=rows;j++)></=></pre></=></=></pre></=i;k++)></pre></=></pre></=></pre></=i;></pre></=></pre></=i;></pre></=i;></pre></row;></pre></4>
Produktion:
7. Mønster-7
import java.util.Scanner; public class Pattern7 { public static void main(String[] args) { int i, j, n; Scanner sc = new Scanner(System.in); System.out.print('Enter the number of rows you want to print: '); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+' '); } System.out.println(); } } }
Produktion:
8. Mønster-8
public class Pattern8 { public static void main(String[] args) { int rows=9; //number of rows to print for (int i = 1; i <= 1 rows; i++) { int num; if(i%2="=" 0) num="0;" for (int j="1;" <="rows;" j++) system.out.print(num);="="0)?"" : 0; } else system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-24.webp" alt="How to Print Pattern in Java"> <p> <strong>9. Pattern-9</strong> </p> <pre> import java.util.Scanner; public class Pattern9 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { for (j="1;" j <="i;" j++) if(j%2="=" 0) system.out.print(0); } else system.out.print(1); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-25.webp" alt="How to Print Pattern in Java"> <p> <strong>10. Pattern-10</strong> </p> <pre> public class Pattern10 { public static void main(String[] args) { int n = 10; for (int i = 1; i <= n; i++) { for (int j="1;" < i; j++) system.out.print(\\\\\\\\\\\\\' \\\\\\\\\\\\\'); } k="i;" k++) system.out.print(k+\\\\\\\\\\\\\' system.out.println(); i="n-1;">= 1; i--) { for (int j = 1; j <i; j++) { system.out.print(\\\\\\\\\\\\\' \\\\\\\\\\\\\'); } for (int k="i;" <="n;" k++) system.out.print(k+\\\\\\\\\\\\\' system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-26.webp" alt="How to Print Pattern in Java"> <p> <strong>11. Pattern-11</strong> </p> <pre> public class Pattern11 { public static void main(String[] args) { int rows=8; //Prints upper half pattern for (int i = 1; i <= rows; i++) { for (int j="1;" <="i;" j++) system.out.print(j+\\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); prints lower half pattern i="rows-1;">= 1; i--) { for (int j = 1; j <= i; j++) { system.out.print(j+\\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-27.webp" alt="How to Print Pattern in Java"> <p> <strong>12. Pattern-12</strong> </p> <pre> public class Pattern12 { public static void main(String[] args) { int rows=9; for (int i = 1; i = i; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-28.webp" alt="How to Print Pattern in Java"> <p> <strong>13. Pattern-13</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int i, j, rows=9; for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-29.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Pattern-14</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int rows=7, i, j; for (i = rows; i >= 1; i--) { for (j = i; j >= 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-30.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Pattern-15</strong> </p> <pre> public class Pattern15 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { prints first half of the row for (j="1;" j="1;" j--) system.out.print(j+\\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-31.webp" alt="How to Print Pattern in Java"> <p> <strong>16. Pattern-16</strong> </p> <pre> public class Pattern16 { public static void main(String[] args) { int i, j, rows=9; //Prints upper half pattern for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); prints lower half pattern for (i="2;" i <="rows;" i++) (j="1;" j pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-32.webp" alt="How to Print Pattern in Java"> <p> <strong>17. Pattern-17</strong> </p> <pre> public class Pattern17 { public static void main(String[] args) { int rows=9; //Prints upper half pattern for (int i = 1; i <= rows; i++) { prints i spaces at the beginning of each row for (int j="1;" < i; j++) system.out.print(\\\\\\\\\\\\\' \\\\\\\\\\\\\'); } to rows value end system.out.print(j); system.out.println(); lower half pattern>= 1; i--) { //Prints i spaces at the beginning of each row for (int j = 1; j <i; j++) { system.out.print(\\\\\\\\\\\\\' \\\\\\\\\\\\\'); } prints i to rows value at the end of each row for (int j="i;" <="rows;" system.out.print(j); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-33.webp" alt="How to Print Pattern in Java"> <p> <strong>18. Pattern-18</strong> </p> <pre> public class Pattern18 { public static void main(String[] args) { int rows=8; for (int i = 1; i <= rows; i++) { for (int j="1;" <="rows-i;" j++) system.out.print(1); } system.out.print(i); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-34.webp" alt="How to Print Pattern in Java"> <p> <strong>19. Pattern-19</strong> </p> <pre> public class Pattern19 { public static void main(String args[]) { int rows=9; for (int i = 1; i <= rows; i++) { int num="i;" for (int j="1;" <="i;" j++) system.out.print(num+\\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-35.webp" alt="How to Print Pattern in Java"> <p> <strong>20. Pattern-20</strong> </p> <pre> public class Pattern20 { public static void main(String[] args) { int i, j, k, rows=9; for(i=1;i<rows+1 ;i++) { for(j="i;" j < rows+1 ;j++) system.out.print(j + \\\\\\\\\\\\\' \\\\\\\\\\\\\'); } for(k="1;" k i ;k++) system.out.print(k system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-36.webp" alt="How to Print Pattern in Java"> <p> <strong>21. Pattern-21</strong> </p> <pre> import java.util.Scanner; public class Pattern21 { public static void main(String[] args) { int i, j, min, n; //n is the number up to you want to print System.out.print('Enter the value of n: '); Scanner sc= new Scanner(System.in); n=sc.nextInt(); //loo for upper left part for (i = 1; i <= n; i++) { for (j="1;" j <="n;" j++) min="i" <j ? i : j; system.out.print(n - + 1+ \\\\\\\\\\\\\' \\\\\\\\\\\\\'); } loop upper right part 1;>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); loop for lower left part (i="n" 1;>= 1; i--) { for (j = 1; j <= n; j++) { min="i" <j ? i : j; system.out.print(n - + 1+ \\\\\\\\\\\\\' \\\\\\\\\\\\\'); } loop for lower right part (j="n" 1; j>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-37.webp" alt="How to Print Pattern in Java"> <h2>Character Pattern</h2> <p> <strong>1. Right Triangle Alphabetic Pattern</strong> </p> <pre> public class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 //inner loop for rows for (int i = 0; i <= 8; i++) { outer loop for columns (int j="0;" <="i;" j++) adds the value of in ascii a and prints corresponding alphabet system.out.print((char) (alphabet + j) \\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-38.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Repeating Alphabet Pattern</strong> </p> <pre> public class RepeatingPattern { public static void main(String[] args) { int letter = 65; //ASCII value of capital A is 65 //inner loop for rwos for (int i = 0; i<= 9; i++) { outer loop for columns (int j="0;" <="i;" j++) prints the character system.out.print((char) letter + \\\\\\\\\\\\\' \\\\\\\\\\\\\'); } letter++; system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-39.webp" alt="How to Print Pattern in Java"> <p> <strong>3. K-shape Alphabet Pattern</strong> </p> <pre> public class KshapePattern { public static void main(String[] args) { for (int i = 8; i >= 0; i--) { int alphabet = 65; for (int j = 0; j <= i; j++) { system.out.print((char) (alphabet + j) \\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); for (int i="0;" i<="8;" i++) int alphabet="65;" j="0;" <="i;" pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-40.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Triangle Character Pattern</strong> </p> <pre> public class TriangleCharacterPattern { public static void main(String[] args) { for (int i = 0; i i; j--) { System.out.print(' '); } for (int k = 0; k <= i; k++) { system.out.print((char) (alphabet + k) \\\\\\\\\\\\\' \\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-41.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Diamond Character Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondCharacterPattern { public static void main(String[] args) { char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print('Enter a Character between A to Z : '); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next('[A-Z]').charAt(0); // search for letter number in the array letter for (int i = 0; i <alphabet.length; i++) { if (letter[i]="=" user_ alphabet) alphabet _number="i;" break; } construct diamond for (int i="0;" <="alphabet" _number; diamond[i] ; add initial spaces j="0;" - i; j++) +=" " space between letters (alphabet[i] !="A" ) <2 * 1; draw the first part of system.out.println(diamond[i]);>= 0; i--) { // Draw the second part of the diamond // prints the diamondArray in the reverse order System.out.println(diamond[i]); } } catch (Exception e) { e.printStackTrace(); } finally { reader.close(); } } } </alphabet.length;></pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-42.webp" alt="How to Print Pattern in Java"> <hr></=></pre></=></pre></=></pre></=></pre></j></=></j></=></pre></rows+1></pre></=></pre></=></pre></i;></=></pre></=></pre></=></pre></=></pre></=></=></pre></i;></=></pre></=></pre></=>
Produktion:
13. Mønster-13
public class Pattern14 { public static void main(String[] args) { int i, j, rows=9; for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-29.webp" alt="How to Print Pattern in Java"> <p> <strong>14. Pattern-14</strong> </p> <pre> public class Pattern14 { public static void main(String[] args) { int rows=7, i, j; for (i = rows; i >= 1; i--) { for (j = i; j >= 1; j--) { System.out.print(j+' '); } System.out.println(); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-30.webp" alt="How to Print Pattern in Java"> <p> <strong>15. Pattern-15</strong> </p> <pre> public class Pattern15 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { prints first half of the row for (j="1;" j="1;" j--) system.out.print(j+\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-31.webp" alt="How to Print Pattern in Java"> <p> <strong>16. Pattern-16</strong> </p> <pre> public class Pattern16 { public static void main(String[] args) { int i, j, rows=9; //Prints upper half pattern for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } system.out.println(); prints lower half pattern for (i="2;" i <="rows;" i++) (j="1;" j pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-32.webp" alt="How to Print Pattern in Java"> <p> <strong>17. Pattern-17</strong> </p> <pre> public class Pattern17 { public static void main(String[] args) { int rows=9; //Prints upper half pattern for (int i = 1; i <= rows; i++) { prints i spaces at the beginning of each row for (int j="1;" < i; j++) system.out.print(\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } to rows value end system.out.print(j); system.out.println(); lower half pattern>= 1; i--) { //Prints i spaces at the beginning of each row for (int j = 1; j <i; j++) { system.out.print(\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } prints i to rows value at the end of each row for (int j="i;" <="rows;" system.out.print(j); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-33.webp" alt="How to Print Pattern in Java"> <p> <strong>18. Pattern-18</strong> </p> <pre> public class Pattern18 { public static void main(String[] args) { int rows=8; for (int i = 1; i <= rows; i++) { for (int j="1;" <="rows-i;" j++) system.out.print(1); } system.out.print(i); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-34.webp" alt="How to Print Pattern in Java"> <p> <strong>19. Pattern-19</strong> </p> <pre> public class Pattern19 { public static void main(String args[]) { int rows=9; for (int i = 1; i <= rows; i++) { int num="i;" for (int j="1;" <="i;" j++) system.out.print(num+\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-35.webp" alt="How to Print Pattern in Java"> <p> <strong>20. Pattern-20</strong> </p> <pre> public class Pattern20 { public static void main(String[] args) { int i, j, k, rows=9; for(i=1;i<rows+1 ;i++) { for(j="i;" j < rows+1 ;j++) system.out.print(j + \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } for(k="1;" k i ;k++) system.out.print(k system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-36.webp" alt="How to Print Pattern in Java"> <p> <strong>21. Pattern-21</strong> </p> <pre> import java.util.Scanner; public class Pattern21 { public static void main(String[] args) { int i, j, min, n; //n is the number up to you want to print System.out.print('Enter the value of n: '); Scanner sc= new Scanner(System.in); n=sc.nextInt(); //loo for upper left part for (i = 1; i <= n; i++) { for (j="1;" j <="n;" j++) min="i" <j ? i : j; system.out.print(n - + 1+ \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } loop upper right part 1;>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } system.out.println(); loop for lower left part (i="n" 1;>= 1; i--) { for (j = 1; j <= n; j++) { min="i" <j ? i : j; system.out.print(n - + 1+ \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } loop for lower right part (j="n" 1; j>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-37.webp" alt="How to Print Pattern in Java"> <h2>Character Pattern</h2> <p> <strong>1. Right Triangle Alphabetic Pattern</strong> </p> <pre> public class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 //inner loop for rows for (int i = 0; i <= 8; i++) { outer loop for columns (int j="0;" <="i;" j++) adds the value of in ascii a and prints corresponding alphabet system.out.print((char) (alphabet + j) \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-38.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Repeating Alphabet Pattern</strong> </p> <pre> public class RepeatingPattern { public static void main(String[] args) { int letter = 65; //ASCII value of capital A is 65 //inner loop for rwos for (int i = 0; i<= 9; i++) { outer loop for columns (int j="0;" <="i;" j++) prints the character system.out.print((char) letter + \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } letter++; system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-39.webp" alt="How to Print Pattern in Java"> <p> <strong>3. K-shape Alphabet Pattern</strong> </p> <pre> public class KshapePattern { public static void main(String[] args) { for (int i = 8; i >= 0; i--) { int alphabet = 65; for (int j = 0; j <= i; j++) { system.out.print((char) (alphabet + j) \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } system.out.println(); for (int i="0;" i<="8;" i++) int alphabet="65;" j="0;" <="i;" pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-40.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Triangle Character Pattern</strong> </p> <pre> public class TriangleCharacterPattern { public static void main(String[] args) { for (int i = 0; i i; j--) { System.out.print(' '); } for (int k = 0; k <= i; k++) { system.out.print((char) (alphabet + k) \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-41.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Diamond Character Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondCharacterPattern { public static void main(String[] args) { char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print('Enter a Character between A to Z : '); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next('[A-Z]').charAt(0); // search for letter number in the array letter for (int i = 0; i <alphabet.length; i++) { if (letter[i]="=" user_ alphabet) alphabet _number="i;" break; } construct diamond for (int i="0;" <="alphabet" _number; diamond[i] ; add initial spaces j="0;" - i; j++) +=" " space between letters (alphabet[i] !="A" ) <2 * 1; draw the first part of system.out.println(diamond[i]);>= 0; i--) { // Draw the second part of the diamond // prints the diamondArray in the reverse order System.out.println(diamond[i]); } } catch (Exception e) { e.printStackTrace(); } finally { reader.close(); } } } </alphabet.length;></pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-42.webp" alt="How to Print Pattern in Java"> <hr></=></pre></=></pre></=></pre></=></pre></j></=></j></=></pre></rows+1></pre></=></pre></=></pre></i;></=></pre></=></pre></=></pre></=>
Produktion:
15. Mønster-15
public class Pattern15 { public static void main(String[] args) { int i, j, rows=9; for (i = 1; i <= rows; i++) { prints first half of the row for (j="1;" j="1;" j--) system.out.print(j+\\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-31.webp" alt="How to Print Pattern in Java"> <p> <strong>16. Pattern-16</strong> </p> <pre> public class Pattern16 { public static void main(String[] args) { int i, j, rows=9; //Prints upper half pattern for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { system.out.print(j+\\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } system.out.println(); prints lower half pattern for (i="2;" i <="rows;" i++) (j="1;" j pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-32.webp" alt="How to Print Pattern in Java"> <p> <strong>17. Pattern-17</strong> </p> <pre> public class Pattern17 { public static void main(String[] args) { int rows=9; //Prints upper half pattern for (int i = 1; i <= rows; i++) { prints i spaces at the beginning of each row for (int j="1;" < i; j++) system.out.print(\\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } to rows value end system.out.print(j); system.out.println(); lower half pattern>= 1; i--) { //Prints i spaces at the beginning of each row for (int j = 1; j <i; j++) { system.out.print(\\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } prints i to rows value at the end of each row for (int j="i;" <="rows;" system.out.print(j); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-33.webp" alt="How to Print Pattern in Java"> <p> <strong>18. Pattern-18</strong> </p> <pre> public class Pattern18 { public static void main(String[] args) { int rows=8; for (int i = 1; i <= rows; i++) { for (int j="1;" <="rows-i;" j++) system.out.print(1); } system.out.print(i); system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-34.webp" alt="How to Print Pattern in Java"> <p> <strong>19. Pattern-19</strong> </p> <pre> public class Pattern19 { public static void main(String args[]) { int rows=9; for (int i = 1; i <= rows; i++) { int num="i;" for (int j="1;" <="i;" j++) system.out.print(num+\\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-35.webp" alt="How to Print Pattern in Java"> <p> <strong>20. Pattern-20</strong> </p> <pre> public class Pattern20 { public static void main(String[] args) { int i, j, k, rows=9; for(i=1;i<rows+1 ;i++) { for(j="i;" j < rows+1 ;j++) system.out.print(j + \\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } for(k="1;" k i ;k++) system.out.print(k system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-36.webp" alt="How to Print Pattern in Java"> <p> <strong>21. Pattern-21</strong> </p> <pre> import java.util.Scanner; public class Pattern21 { public static void main(String[] args) { int i, j, min, n; //n is the number up to you want to print System.out.print('Enter the value of n: '); Scanner sc= new Scanner(System.in); n=sc.nextInt(); //loo for upper left part for (i = 1; i <= n; i++) { for (j="1;" j <="n;" j++) min="i" <j ? i : j; system.out.print(n - + 1+ \\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } loop upper right part 1;>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } system.out.println(); loop for lower left part (i="n" 1;>= 1; i--) { for (j = 1; j <= n; j++) { min="i" <j ? i : j; system.out.print(n - + 1+ \\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } loop for lower right part (j="n" 1; j>= 1; j--) { min = i <j ? i : j; system.out.print(n - min + 1+ \\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-37.webp" alt="How to Print Pattern in Java"> <h2>Character Pattern</h2> <p> <strong>1. Right Triangle Alphabetic Pattern</strong> </p> <pre> public class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 //inner loop for rows for (int i = 0; i <= 8; i++) { outer loop for columns (int j="0;" <="i;" j++) adds the value of in ascii a and prints corresponding alphabet system.out.print((char) (alphabet + j) \\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-38.webp" alt="How to Print Pattern in Java"> <p> <strong>2. Repeating Alphabet Pattern</strong> </p> <pre> public class RepeatingPattern { public static void main(String[] args) { int letter = 65; //ASCII value of capital A is 65 //inner loop for rwos for (int i = 0; i<= 9; i++) { outer loop for columns (int j="0;" <="i;" j++) prints the character system.out.print((char) letter + \\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } letter++; system.out.println(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-39.webp" alt="How to Print Pattern in Java"> <p> <strong>3. K-shape Alphabet Pattern</strong> </p> <pre> public class KshapePattern { public static void main(String[] args) { for (int i = 8; i >= 0; i--) { int alphabet = 65; for (int j = 0; j <= i; j++) { system.out.print((char) (alphabet + j) \\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } system.out.println(); for (int i="0;" i<="8;" i++) int alphabet="65;" j="0;" <="i;" pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-40.webp" alt="How to Print Pattern in Java"> <p> <strong>4. Triangle Character Pattern</strong> </p> <pre> public class TriangleCharacterPattern { public static void main(String[] args) { for (int i = 0; i i; j--) { System.out.print(' '); } for (int k = 0; k <= i; k++) { system.out.print((char) (alphabet + k) \\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\'); } system.out.println(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-41.webp" alt="How to Print Pattern in Java"> <p> <strong>5. Diamond Character Pattern</strong> </p> <pre> import java.util.Scanner; public class DiamondCharacterPattern { public static void main(String[] args) { char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print('Enter a Character between A to Z : '); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next('[A-Z]').charAt(0); // search for letter number in the array letter for (int i = 0; i <alphabet.length; i++) { if (letter[i]="=" user_ alphabet) alphabet _number="i;" break; } construct diamond for (int i="0;" <="alphabet" _number; diamond[i] ; add initial spaces j="0;" - i; j++) +=" " space between letters (alphabet[i] !="A" ) <2 * 1; draw the first part of system.out.println(diamond[i]);>= 0; i--) { // Draw the second part of the diamond // prints the diamondArray in the reverse order System.out.println(diamond[i]); } } catch (Exception e) { e.printStackTrace(); } finally { reader.close(); } } } </alphabet.length;></pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/88/how-print-pattern-java-42.webp" alt="How to Print Pattern in Java"> <hr></=></pre></=></pre></=></pre></=></pre></j></=></j></=></pre></rows+1></pre></=></pre></=></pre></i;></=></pre></=></pre></=>
Produktion:
=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>2*(i>2*(i>=>=n;>=>=rows;j++)>=>=>=>=i;k++)>=>=>=i;>=>=i;>=i;>4>4>4>4>4>