SlideShare a Scribd company logo
1 of 10
Download to read offline
Programs for printing pyramid patterns in Java
This article is aimed at giving a Java implementation for pattern printing.
Simple pyramid pattern
filter_none
edit
play_arrow
brightness_4
import java.io.*;
// Java code to demonstrate star patterns
public class My_Class
{
// Function to demonstrate printing pattern
public static void printStars(int n)
{
int i, j;
// outer loop to handle number of rows
// n in this case
for(i=0; i<n; i++)
{
// inner loop to handle number of columns
// values changing acc. to outer loop
for(j=0; j<=i; j++)
{
// printing stars
System.out.print("* ");
}
// ending line after each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printStars(n);
}
}
Output:
*
* *
* * *
* * * *
* * * * *
After 180 degree rotation
filter_none
edit
play_arrow
brightness_4
import java.io.*;
// Java code to demonstrate star pattern
public class My_Class
{
// Function to demonstrate printing pattern
public static void printStars(int n)
{
int i, j;
// outer loop to handle number of rows
// n in this case
for(i=0; i<n; i++)
{
// inner loop to handle number spaces
// values changing acc. to requirement
for(j=2*(n-i); j>=0; j--)
{
// printing spaces
System.out.print(" ");
}
// inner loop to handle number of columns
// values changing acc. to outer loop
for(j=0; j<=i; j++)
{
// printing stars
System.out.print("* ");
}
// ending line after each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printStars(n);
}
}
Output:
*
* *
* * *
* * * *
* * * * *
Printing Triangle
filter_none
edit
play_arrow
brightness_4
import java.io.*;
// Java code to demonstrate star pattern Print Star pattern in Java
public class My_Class
{
// Function to demonstrate printing pattern
public static void printTriagle(int n)
{
// outer loop to handle number of rows
// n in this case
for (int i=0; i<n; i++)
{
// inner loop to handle number spaces
// values changing acc. to requirement
for (int j=n-i; j>1; j--)
{
// printing spaces
System.out.print(" ");
}
// inner loop to handle number of columns
// values changing acc. to outer loop
for (int j=0; j<=i; j++ )
{
// printing stars
System.out.print("* ");
}
// ending line after each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printTriagle(n);
}
}
Output:
*
* *
* * *
* * * *
* * * * *
Number Pattern
filter_none
edit
play_arrow
brightness_4
import java.io.*;
// Java code to demonstrate number pattern
public class My_Class
{
// Function to demonstrate printing pattern
public static void printNums(int n)
{
int i, j,num;
// outer loop to handle number of rows
// n in this case
for(i=0; i<n; i++)
{
// initialising starting number
num=1;
// inner loop to handle number of columns
// values changing acc. to outer loop
for(j=0; j<=i; j++)
{
// printing num with a space
System.out.print(num+ " ");
//incrementing value of num
num++;
}
// ending line after each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printNums(n);
}
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Numbers without re assigning
filter_none
edit
play_arrow
brightness_4
import java.io.*;
// Java code to demonstrate star pattern
public class My_Class
{
// Function to demonstrate printing pattern
public static void printNums(int n)
{
// initialising starting number
int i, j, num=1;
// outer loop to handle number of rows
// n in this case
for(i=0; i<n; i++)
{
// without re assigning num
// num = 1;
for(j=0; j<=i; j++)
{
// printing num with a space
System.out.print(num+ " ");
// incrementing num at each column
num = num + 1;
}
// ending line after each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printNums(n);
}
}
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

More Related Content

What's hot

Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design Pattern
Asif Tayef
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
Mohamed Ahmed
 

What's hot (20)

Star pattern programs in java Print Star pattern in java and print triangle ...
Star pattern programs in java Print Star pattern in java and  print triangle ...Star pattern programs in java Print Star pattern in java and  print triangle ...
Star pattern programs in java Print Star pattern in java and print triangle ...
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Function
FunctionFunction
Function
 
0.my book draft chap 1
0.my book draft chap 10.my book draft chap 1
0.my book draft chap 1
 
Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design Pattern
 
Replace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeReplace OutputIterator and Extend Range
Replace OutputIterator and Extend Range
 
C++20 features
C++20 features C++20 features
C++20 features
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up Telephony
 
C programming day#2.
C programming day#2.C programming day#2.
C programming day#2.
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
 
Import java
Import javaImport java
Import java
 
C++ references
C++ referencesC++ references
C++ references
 
java program assigment -2
java program assigment -2java program assigment -2
java program assigment -2
 
Catch and throw blocks
Catch and throw blocksCatch and throw blocks
Catch and throw blocks
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Javascript OOP
Javascript OOPJavascript OOP
Javascript OOP
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 

Similar to Print Star pattern in java and print triangle of stars in java

KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
Aiman Hud
 
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
dorisc7
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdf
angelsfashion1
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
mary772
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
mccormicknadine86
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
aptcomputerzone
 

Similar to Print Star pattern in java and print triangle of stars in java (20)

Algoritmos sujei
Algoritmos sujeiAlgoritmos sujei
Algoritmos sujei
 
Problemas secuenciales.
Problemas secuenciales.Problemas secuenciales.
Problemas secuenciales.
 
Problemas secuenciales.
Problemas secuenciales.Problemas secuenciales.
Problemas secuenciales.
 
Problemas secuenciales.
Problemas secuenciales.Problemas secuenciales.
Problemas secuenciales.
 
Problemas secuenciales.
Problemas secuenciales.Problemas secuenciales.
Problemas secuenciales.
 
Problemas secuenciales.
Problemas secuenciales.Problemas secuenciales.
Problemas secuenciales.
 
Estructura secuencial -garcia
Estructura secuencial -garciaEstructura secuencial -garcia
Estructura secuencial -garcia
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Star pattern programs in java Print Star pattern in java and print triangle ...
Star pattern programs in java Print Star pattern in java and  print triangle ...Star pattern programs in java Print Star pattern in java and  print triangle ...
Star pattern programs in java Print Star pattern in java and print triangle ...
 
Presentation1 computer shaan
Presentation1 computer shaanPresentation1 computer shaan
Presentation1 computer shaan
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
 
java program assigment -1
java program assigment -1java program assigment -1
java program assigment -1
 
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operators
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdf
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
 
Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
 

Recently uploaded

Garia ^ (Call Girls) in Kolkata - Book 8005736733 Call Girls Available 24 Hou...
Garia ^ (Call Girls) in Kolkata - Book 8005736733 Call Girls Available 24 Hou...Garia ^ (Call Girls) in Kolkata - Book 8005736733 Call Girls Available 24 Hou...
Garia ^ (Call Girls) in Kolkata - Book 8005736733 Call Girls Available 24 Hou...
HyderabadDolls
 
[[Nerul]] MNavi Mumbai Honoreble Call Girls Number-9833754194-Panvel Best Es...
[[Nerul]] MNavi Mumbai Honoreble  Call Girls Number-9833754194-Panvel Best Es...[[Nerul]] MNavi Mumbai Honoreble  Call Girls Number-9833754194-Panvel Best Es...
[[Nerul]] MNavi Mumbai Honoreble Call Girls Number-9833754194-Panvel Best Es...
priyasharma62062
 
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Bhayandar Capable Call Girls ,07506202331,Mira Road Beautiful Call Girl
Bhayandar Capable Call Girls ,07506202331,Mira Road Beautiful Call GirlBhayandar Capable Call Girls ,07506202331,Mira Road Beautiful Call Girl
Bhayandar Capable Call Girls ,07506202331,Mira Road Beautiful Call Girl
Priya Reddy
 

Recently uploaded (20)

Garia ^ (Call Girls) in Kolkata - Book 8005736733 Call Girls Available 24 Hou...
Garia ^ (Call Girls) in Kolkata - Book 8005736733 Call Girls Available 24 Hou...Garia ^ (Call Girls) in Kolkata - Book 8005736733 Call Girls Available 24 Hou...
Garia ^ (Call Girls) in Kolkata - Book 8005736733 Call Girls Available 24 Hou...
 
Lion One Corporate Presentation May 2024
Lion One Corporate Presentation May 2024Lion One Corporate Presentation May 2024
Lion One Corporate Presentation May 2024
 
Kurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
Kurla Capable Call Girls ,07506202331, Sion Affordable Call GirlsKurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
Kurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
 
cost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptxcost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptx
 
Premium Call Girls Bangalore Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
Premium Call Girls Bangalore Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...Premium Call Girls Bangalore Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
Premium Call Girls Bangalore Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
Effortless Income Tax Filing Online Your Path to Financial Ease..pdf
Effortless Income Tax Filing Online Your Path to Financial Ease..pdfEffortless Income Tax Filing Online Your Path to Financial Ease..pdf
Effortless Income Tax Filing Online Your Path to Financial Ease..pdf
 
[[Nerul]] MNavi Mumbai Honoreble Call Girls Number-9833754194-Panvel Best Es...
[[Nerul]] MNavi Mumbai Honoreble  Call Girls Number-9833754194-Panvel Best Es...[[Nerul]] MNavi Mumbai Honoreble  Call Girls Number-9833754194-Panvel Best Es...
[[Nerul]] MNavi Mumbai Honoreble Call Girls Number-9833754194-Panvel Best Es...
 
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
 
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Test bank for advanced assessment interpreting findings and formulating diffe...
Test bank for advanced assessment interpreting findings and formulating diffe...Test bank for advanced assessment interpreting findings and formulating diffe...
Test bank for advanced assessment interpreting findings and formulating diffe...
 
✂️ 👅 Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...
✂️ 👅 Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...✂️ 👅 Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...
✂️ 👅 Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...
 
✂️ 👅 Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
✂️ 👅 Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...✂️ 👅 Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
✂️ 👅 Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
 
Technology industry / Finnish economic outlook
Technology industry / Finnish economic outlookTechnology industry / Finnish economic outlook
Technology industry / Finnish economic outlook
 
Collecting banker, Capacity of collecting Banker, conditions under section 13...
Collecting banker, Capacity of collecting Banker, conditions under section 13...Collecting banker, Capacity of collecting Banker, conditions under section 13...
Collecting banker, Capacity of collecting Banker, conditions under section 13...
 
20240419-SMC-submission-Annual-Superannuation-Performance-Test-–-design-optio...
20240419-SMC-submission-Annual-Superannuation-Performance-Test-–-design-optio...20240419-SMC-submission-Annual-Superannuation-Performance-Test-–-design-optio...
20240419-SMC-submission-Annual-Superannuation-Performance-Test-–-design-optio...
 
Turbhe Fantastic Escorts📞📞9833754194 Kopar Khairane Marathi Call Girls-Kopar ...
Turbhe Fantastic Escorts📞📞9833754194 Kopar Khairane Marathi Call Girls-Kopar ...Turbhe Fantastic Escorts📞📞9833754194 Kopar Khairane Marathi Call Girls-Kopar ...
Turbhe Fantastic Escorts📞📞9833754194 Kopar Khairane Marathi Call Girls-Kopar ...
 
Pension dashboards forum 1 May 2024 (1).pdf
Pension dashboards forum 1 May 2024 (1).pdfPension dashboards forum 1 May 2024 (1).pdf
Pension dashboards forum 1 May 2024 (1).pdf
 
Solution Manual For Financial Statement Analysis, 13th Edition By Charles H. ...
Solution Manual For Financial Statement Analysis, 13th Edition By Charles H. ...Solution Manual For Financial Statement Analysis, 13th Edition By Charles H. ...
Solution Manual For Financial Statement Analysis, 13th Edition By Charles H. ...
 
Bhubaneswar🌹Kalpana Mesuem ❤CALL GIRLS 9777949614 💟 CALL GIRLS IN bhubaneswa...
Bhubaneswar🌹Kalpana Mesuem  ❤CALL GIRLS 9777949614 💟 CALL GIRLS IN bhubaneswa...Bhubaneswar🌹Kalpana Mesuem  ❤CALL GIRLS 9777949614 💟 CALL GIRLS IN bhubaneswa...
Bhubaneswar🌹Kalpana Mesuem ❤CALL GIRLS 9777949614 💟 CALL GIRLS IN bhubaneswa...
 
Bhayandar Capable Call Girls ,07506202331,Mira Road Beautiful Call Girl
Bhayandar Capable Call Girls ,07506202331,Mira Road Beautiful Call GirlBhayandar Capable Call Girls ,07506202331,Mira Road Beautiful Call Girl
Bhayandar Capable Call Girls ,07506202331,Mira Road Beautiful Call Girl
 

Print Star pattern in java and print triangle of stars in java

  • 1. Programs for printing pyramid patterns in Java This article is aimed at giving a Java implementation for pattern printing. Simple pyramid pattern filter_none edit play_arrow brightness_4 import java.io.*; // Java code to demonstrate star patterns public class My_Class { // Function to demonstrate printing pattern public static void printStars(int n) { int i, j; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // inner loop to handle number of columns // values changing acc. to outer loop for(j=0; j<=i; j++) {
  • 2. // printing stars System.out.print("* "); } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printStars(n); } } Output: * * * * * * * * * * * * * * * After 180 degree rotation
  • 3. filter_none edit play_arrow brightness_4 import java.io.*; // Java code to demonstrate star pattern public class My_Class { // Function to demonstrate printing pattern public static void printStars(int n) { int i, j; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // inner loop to handle number spaces // values changing acc. to requirement for(j=2*(n-i); j>=0; j--) { // printing spaces System.out.print(" "); }
  • 4. // inner loop to handle number of columns // values changing acc. to outer loop for(j=0; j<=i; j++) { // printing stars System.out.print("* "); } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printStars(n); } } Output: * * * * * * * * * * * * * * * Printing Triangle
  • 5. filter_none edit play_arrow brightness_4 import java.io.*; // Java code to demonstrate star pattern Print Star pattern in Java public class My_Class { // Function to demonstrate printing pattern public static void printTriagle(int n) { // outer loop to handle number of rows // n in this case for (int i=0; i<n; i++) { // inner loop to handle number spaces // values changing acc. to requirement for (int j=n-i; j>1; j--) { // printing spaces System.out.print(" "); } // inner loop to handle number of columns // values changing acc. to outer loop
  • 6. for (int j=0; j<=i; j++ ) { // printing stars System.out.print("* "); } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printTriagle(n); } } Output: * * * * * * * * * * * * * * * Number Pattern filter_none
  • 7. edit play_arrow brightness_4 import java.io.*; // Java code to demonstrate number pattern public class My_Class { // Function to demonstrate printing pattern public static void printNums(int n) { int i, j,num; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // initialising starting number num=1; // inner loop to handle number of columns // values changing acc. to outer loop for(j=0; j<=i; j++) { // printing num with a space System.out.print(num+ " "); //incrementing value of num
  • 8. num++; } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printNums(n); } } Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Numbers without re assigning filter_none edit play_arrow
  • 9. brightness_4 import java.io.*; // Java code to demonstrate star pattern public class My_Class { // Function to demonstrate printing pattern public static void printNums(int n) { // initialising starting number int i, j, num=1; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // without re assigning num // num = 1; for(j=0; j<=i; j++) { // printing num with a space System.out.print(num+ " "); // incrementing num at each column num = num + 1; } // ending line after each row
  • 10. System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printNums(n); } } Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15