logo

Kuinka tulostaa kuvio Javassa

Java malliohjelma parantaa koodaustaitoa, logiikkaa ja silmukkakäsitteitä. Sitä kysytään enimmäkseen Java haastattelu tarkistaaksesi ohjelmoijan logiikan ja ajattelun. Voimme tulostaa a Java malliohjelma eri malleissa. Malliohjelman oppimiseksi meillä on oltava syvät Java-silmukan tuntemus, kuten varten silmukka tehdä, kun silmukka. Tässä osiossa opimme kuinka tulostaa kuvio Javassa .

Olemme luokitelleet Java malliohjelma kolmeen kategoriaan:

    Aloita kuvio Numerokavio Hahmon kuvio

Ennen kuin siirryt kuvioohjelmiin, katsotaanpa lähestymistapaa.

Aina kun suunnittelet logiikkaa kuvioohjelmalle, piirrä ensin se kuvio lohkoihin, kuten olemme osoittaneet seuraavassa kuvassa. Kuvassa on selkeä ilme kuviosta.

Jokaisessa kuvioohjelmassa on kaksi tai useampia kuin kaksi silmukkaa. Silmukan lukumäärä riippuu kuvion tai logiikan monimutkaisuudesta. Ensimmäinen for-silmukka toimii riville ja toinen silmukka sarakkeelle. Malliohjelmissa Java silmukalle on laajalti käytössä.

Kuinka tulostaa kuvio Javassa

Yllä olevassa mallissa rivi on merkitty i ja sarakkeessa on merkitty j . Näemme, että ensimmäinen rivi tulostaa vain tähden. Toiselle riville tulostetaan kaksi tähteä ja niin edelleen. The värillinen lohkojen tulostaa tilat .

aakkoset numeroina

Luodaan kuviolle logiikka, anna yllä. Seuraavassa koodinpätkässä aloitamme rivin ja sarakkeen arvon 0:sta. Voimme myös aloittaa sen 1:stä, se on sinun valintasi.

 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&apos;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(&apos; &apos;); } //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(&apos; &apos;); } //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(&apos;Enter the number of rows you want to print: &apos;); 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&gt;=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&lt;=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&gt;= 1; i--) { for (int j=row; j&gt;i;j--) { System.out.print(&apos; &apos;); } 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&apos;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(&apos;Enter the number of rows you want to print: &apos;); 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&apos;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(&apos;Enter the number of rows you want to print: &apos;); 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(&apos;Enter the number of rows you want to print: &apos;); 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&gt;= 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(&apos;Enter the number of rows you want to print: &apos;); int rows = sc.nextInt(); for (i=1; i i ; j--) { System.out.print(&apos; &apos;); } System.out.print(&apos;*&apos;); 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 &gt; i ; j--) { System.out.print(&apos; &apos;); } System.out.print(&apos;*&apos;); 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(&apos;Enter the number of rows you want to print: &apos;); 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(&apos;Enter the number of rows youy want to print: &apos;); rows = sc.nextInt(); for (i = rows; i &gt;= 1; i--) { for (j = rows; j &gt;= i; j--) { System.out.print(j+&apos; &apos;); } 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(&apos;Enter the number of rows you want to print: &apos;); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+&apos; &apos;); } 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+&apos; &apos;); } 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 &gt;= 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 &gt;= 1; i--) { for (j = i; j &gt;= 1; j--) { System.out.print(j+&apos; &apos;); } 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 &gt;= 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(&apos;Enter the value of n: &apos;); 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 &gt;= 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(&apos; &apos;); } 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 = { &apos;A&apos;, &apos;B&apos;, &apos;C&apos;, &apos;D&apos;, &apos;E&apos;, &apos;F&apos;, &apos;G&apos;, &apos;H&apos;, &apos;I&apos;, &apos;J&apos;, &apos;K&apos;, &apos;L&apos;, &apos;M&apos;, &apos;N&apos;, &apos;O&apos;, &apos;P&apos;, &apos;Q&apos;, &apos;R&apos;, &apos;S&apos;, &apos;T&apos;, &apos;U&apos;, &apos;V&apos;, &apos;W&apos;, &apos;X&apos;, &apos;Y&apos;, &apos;Z&apos; }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print(&apos;Enter a Character between A to Z : &apos;); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next(&apos;[A-Z]&apos;).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;>

Nyt i:n ja j:n arvo kasvaa 1:ksi.

Iteraatio 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(&apos; &apos;); } //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(&apos; &apos;); } //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(&apos;Enter the number of rows you want to print: &apos;); 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&gt;=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&lt;=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&gt;= 1; i--) { for (int j=row; j&gt;i;j--) { System.out.print(&apos; &apos;); } 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&apos;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(&apos;Enter the number of rows you want to print: &apos;); 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&apos;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(&apos;Enter the number of rows you want to print: &apos;); 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(&apos;Enter the number of rows you want to print: &apos;); 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&gt;= 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(&apos;Enter the number of rows you want to print: &apos;); int rows = sc.nextInt(); for (i=1; i i ; j--) { System.out.print(&apos; &apos;); } System.out.print(&apos;*&apos;); 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 &gt; i ; j--) { System.out.print(&apos; &apos;); } System.out.print(&apos;*&apos;); 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(&apos;Enter the number of rows you want to print: &apos;); 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(&apos;Enter the number of rows youy want to print: &apos;); rows = sc.nextInt(); for (i = rows; i &gt;= 1; i--) { for (j = rows; j &gt;= i; j--) { System.out.print(j+&apos; &apos;); } 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(&apos;Enter the number of rows you want to print: &apos;); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+&apos; &apos;); } 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);="=&quot;0)?&quot;" : 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+&apos; &apos;); } 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 &gt;= 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 &gt;= 1; i--) { for (j = i; j &gt;= 1; j--) { System.out.print(j+&apos; &apos;); } 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 &gt;= 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(&apos;Enter the value of n: &apos;); 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 &gt;= 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(&apos; &apos;); } 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 = { &apos;A&apos;, &apos;B&apos;, &apos;C&apos;, &apos;D&apos;, &apos;E&apos;, &apos;F&apos;, &apos;G&apos;, &apos;H&apos;, &apos;I&apos;, &apos;J&apos;, &apos;K&apos;, &apos;L&apos;, &apos;M&apos;, &apos;N&apos;, &apos;O&apos;, &apos;P&apos;, &apos;Q&apos;, &apos;R&apos;, &apos;S&apos;, &apos;T&apos;, &apos;U&apos;, &apos;V&apos;, &apos;W&apos;, &apos;X&apos;, &apos;Y&apos;, &apos;Z&apos; }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print(&apos;Enter a Character between A to Z : &apos;); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next(&apos;[A-Z]&apos;).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>

Nyt i:n ja j:n arvo kasvaa 2:ksi.

Iteraatio 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(&apos; &apos;); } //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(&apos; &apos;); } //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(&apos;Enter the number of rows you want to print: &apos;); 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&gt;=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&lt;=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&gt;= 1; i--) { for (int j=row; j&gt;i;j--) { System.out.print(&apos; &apos;); } 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&apos;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(&apos;Enter the number of rows you want to print: &apos;); 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&apos;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(&apos;Enter the number of rows you want to print: &apos;); 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(&apos;Enter the number of rows you want to print: &apos;); 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&gt;= 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(&apos;Enter the number of rows you want to print: &apos;); int rows = sc.nextInt(); for (i=1; i i ; j--) { System.out.print(&apos; &apos;); } System.out.print(&apos;*&apos;); 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 &gt; i ; j--) { System.out.print(&apos; &apos;); } System.out.print(&apos;*&apos;); 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(&apos;Enter the number of rows you want to print: &apos;); 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(&apos;Enter the number of rows youy want to print: &apos;); rows = sc.nextInt(); for (i = rows; i &gt;= 1; i--) { for (j = rows; j &gt;= i; j--) { System.out.print(j+&apos; &apos;); } 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(&apos;Enter the number of rows you want to print: &apos;); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+&apos; &apos;); } 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);="=&quot;0)?&quot;" : 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+&apos; &apos;); } 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 &gt;= 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 &gt;= 1; i--) { for (j = i; j &gt;= 1; j--) { System.out.print(j+&apos; &apos;); } 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 &gt;= 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(&apos;Enter the value of n: &apos;); 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 &gt;= 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(&apos; &apos;); } 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 = { &apos;A&apos;, &apos;B&apos;, &apos;C&apos;, &apos;D&apos;, &apos;E&apos;, &apos;F&apos;, &apos;G&apos;, &apos;H&apos;, &apos;I&apos;, &apos;J&apos;, &apos;K&apos;, &apos;L&apos;, &apos;M&apos;, &apos;N&apos;, &apos;O&apos;, &apos;P&apos;, &apos;Q&apos;, &apos;R&apos;, &apos;S&apos;, &apos;T&apos;, &apos;U&apos;, &apos;V&apos;, &apos;W&apos;, &apos;X&apos;, &apos;Y&apos;, &apos;Z&apos; }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print(&apos;Enter a Character between A to Z : &apos;); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next(&apos;[A-Z]&apos;).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>

Nyt i:n ja j:n arvo kasvaa arvoon 3.

Iteraatio 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(&apos; &apos;); } //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(&apos; &apos;); } //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(&apos;Enter the number of rows you want to print: &apos;); 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&gt;=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&lt;=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&gt;= 1; i--) { for (int j=row; j&gt;i;j--) { System.out.print(&apos; &apos;); } 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&apos;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(&apos;Enter the number of rows you want to print: &apos;); 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&apos;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(&apos;Enter the number of rows you want to print: &apos;); 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(&apos;Enter the number of rows you want to print: &apos;); 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&gt;= 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(&apos;Enter the number of rows you want to print: &apos;); int rows = sc.nextInt(); for (i=1; i i ; j--) { System.out.print(&apos; &apos;); } System.out.print(&apos;*&apos;); 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 &gt; i ; j--) { System.out.print(&apos; &apos;); } System.out.print(&apos;*&apos;); 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(&apos;Enter the number of rows you want to print: &apos;); 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(&apos;Enter the number of rows youy want to print: &apos;); rows = sc.nextInt(); for (i = rows; i &gt;= 1; i--) { for (j = rows; j &gt;= i; j--) { System.out.print(j+&apos; &apos;); } 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(&apos;Enter the number of rows you want to print: &apos;); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+&apos; &apos;); } 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);="=&quot;0)?&quot;" : 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+&apos; &apos;); } 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 &gt;= 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 &gt;= 1; i--) { for (j = i; j &gt;= 1; j--) { System.out.print(j+&apos; &apos;); } 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 &gt;= 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(&apos;Enter the value of n: &apos;); 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 &gt;= 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(&apos; &apos;); } 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 = { &apos;A&apos;, &apos;B&apos;, &apos;C&apos;, &apos;D&apos;, &apos;E&apos;, &apos;F&apos;, &apos;G&apos;, &apos;H&apos;, &apos;I&apos;, &apos;J&apos;, &apos;K&apos;, &apos;L&apos;, &apos;M&apos;, &apos;N&apos;, &apos;O&apos;, &apos;P&apos;, &apos;Q&apos;, &apos;R&apos;, &apos;S&apos;, &apos;T&apos;, &apos;U&apos;, &apos;V&apos;, &apos;W&apos;, &apos;X&apos;, &apos;Y&apos;, &apos;Z&apos; }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print(&apos;Enter a Character between A to Z : &apos;); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next(&apos;[A-Z]&apos;).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>

Nyt i:n ja j:n arvo kasvaa 4:ään.

 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(&apos; &apos;); } //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(&apos; &apos;); } //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(&apos;Enter the number of rows you want to print: &apos;); 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&gt;=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&lt;=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&gt;= 1; i--) { for (int j=row; j&gt;i;j--) { System.out.print(&apos; &apos;); } 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&apos;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(&apos;Enter the number of rows you want to print: &apos;); 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&apos;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(&apos;Enter the number of rows you want to print: &apos;); 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(&apos;Enter the number of rows you want to print: &apos;); 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&gt;= 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(&apos;Enter the number of rows you want to print: &apos;); int rows = sc.nextInt(); for (i=1; i i ; j--) { System.out.print(&apos; &apos;); } System.out.print(&apos;*&apos;); 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 &gt; i ; j--) { System.out.print(&apos; &apos;); } System.out.print(&apos;*&apos;); 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(&apos;Enter the number of rows you want to print: &apos;); 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(&apos;Enter the number of rows youy want to print: &apos;); rows = sc.nextInt(); for (i = rows; i &gt;= 1; i--) { for (j = rows; j &gt;= i; j--) { System.out.print(j+&apos; &apos;); } 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(&apos;Enter the number of rows you want to print: &apos;); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+&apos; &apos;); } 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);="=&quot;0)?&quot;" : 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+&apos; &apos;); } 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 &gt;= 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 &gt;= 1; i--) { for (j = i; j &gt;= 1; j--) { System.out.print(j+&apos; &apos;); } 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 &gt;= 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(&apos;Enter the value of n: &apos;); 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 &gt;= 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(&apos; &apos;); } 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 = { &apos;A&apos;, &apos;B&apos;, &apos;C&apos;, &apos;D&apos;, &apos;E&apos;, &apos;F&apos;, &apos;G&apos;, &apos;H&apos;, &apos;I&apos;, &apos;J&apos;, &apos;K&apos;, &apos;L&apos;, &apos;M&apos;, &apos;N&apos;, &apos;O&apos;, &apos;P&apos;, &apos;Q&apos;, &apos;R&apos;, &apos;S&apos;, &apos;T&apos;, &apos;U&apos;, &apos;V&apos;, &apos;W&apos;, &apos;X&apos;, &apos;Y&apos;, &apos;Z&apos; }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print(&apos;Enter a Character between A to Z : &apos;); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next(&apos;[A-Z]&apos;).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>

Lähtö:

Kuinka tulostaa kuvio Javassa

7. Kuvio-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(&apos;Enter the number of rows you want to print: &apos;); n = sc.nextInt(); for (i = 1; i = 1; j--) { System.out.print(j+&apos; &apos;); } System.out.println(); } } } 

Lähtö:

Kuinka tulostaa kuvio Javassa

8. Kuvio-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);="=&quot;0)?&quot;" : 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+&apos; &apos;); } 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 &gt;= 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 &gt;= 1; i--) { for (j = i; j &gt;= 1; j--) { System.out.print(j+&apos; &apos;); } 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 &gt;= 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(&apos;Enter the value of n: &apos;); 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 &gt;= 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(&apos; &apos;); } 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 = { &apos;A&apos;, &apos;B&apos;, &apos;C&apos;, &apos;D&apos;, &apos;E&apos;, &apos;F&apos;, &apos;G&apos;, &apos;H&apos;, &apos;I&apos;, &apos;J&apos;, &apos;K&apos;, &apos;L&apos;, &apos;M&apos;, &apos;N&apos;, &apos;O&apos;, &apos;P&apos;, &apos;Q&apos;, &apos;R&apos;, &apos;S&apos;, &apos;T&apos;, &apos;U&apos;, &apos;V&apos;, &apos;W&apos;, &apos;X&apos;, &apos;Y&apos;, &apos;Z&apos; }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print(&apos;Enter a Character between A to Z : &apos;); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next(&apos;[A-Z]&apos;).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></=>

Lähtö:

Kuinka tulostaa kuvio Javassa

13. Kuvio-13

 public class Pattern14 { public static void main(String[] args) { int i, j, rows=9; for (i = rows; i &gt;= 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 &gt;= 1; i--) { for (j = i; j &gt;= 1; j--) { System.out.print(j+&apos; &apos;); } 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 &gt;= 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(&apos;Enter the value of n: &apos;); 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 &gt;= 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(&apos; &apos;); } 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 = { &apos;A&apos;, &apos;B&apos;, &apos;C&apos;, &apos;D&apos;, &apos;E&apos;, &apos;F&apos;, &apos;G&apos;, &apos;H&apos;, &apos;I&apos;, &apos;J&apos;, &apos;K&apos;, &apos;L&apos;, &apos;M&apos;, &apos;N&apos;, &apos;O&apos;, &apos;P&apos;, &apos;Q&apos;, &apos;R&apos;, &apos;S&apos;, &apos;T&apos;, &apos;U&apos;, &apos;V&apos;, &apos;W&apos;, &apos;X&apos;, &apos;Y&apos;, &apos;Z&apos; }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print(&apos;Enter a Character between A to Z : &apos;); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next(&apos;[A-Z]&apos;).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></=>

Lähtö:

Kuinka tulostaa kuvio Javassa

15. Kuvio-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 &gt;= 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(&apos;Enter the value of n: &apos;); 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 &gt;= 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(&apos; &apos;); } 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 = { &apos;A&apos;, &apos;B&apos;, &apos;C&apos;, &apos;D&apos;, &apos;E&apos;, &apos;F&apos;, &apos;G&apos;, &apos;H&apos;, &apos;I&apos;, &apos;J&apos;, &apos;K&apos;, &apos;L&apos;, &apos;M&apos;, &apos;N&apos;, &apos;O&apos;, &apos;P&apos;, &apos;Q&apos;, &apos;R&apos;, &apos;S&apos;, &apos;T&apos;, &apos;U&apos;, &apos;V&apos;, &apos;W&apos;, &apos;X&apos;, &apos;Y&apos;, &apos;Z&apos; }; int alphabet _number = 0; String[] diamond = new String[26]; // array of strings System.out.print(&apos;Enter a Character between A to Z : &apos;); Scanner reader = new Scanner(System.in); try { char user_ alphabet = reader.next(&apos;[A-Z]&apos;).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></=>

Lähtö:

Kuinka tulostaa kuvio Javassa