The pattern forms a right-angled triangle of numbers.
In each row, numbers start from 1 and go up to the row number.
Use an outer loop to handle the rows from 1 to N.
For each row, use an inner loop to print numbers from 1 to the current row index, then move to the next line.
C++
#include<iostream>usingnamespacestd;// Function to print the patternvoidprintPattern(intN){// Loop for rowsfor(inti=1;i<=N;i++){// Print numbers from 1 to ifor(intj=1;j<=i;j++){cout<<j<<" ";}// Move to next linecout<<endl;}}intmain(){intN=5;// Call the functionprintPattern(N);return0;}
Java
publicclassGfG{// Function to print the patternpublicstaticvoidprintPattern(intN){// Loop for rowsfor(inti=1;i<=N;i++){// Print numbers from 1 to ifor(intj=1;j<=i;j++){System.out.print(j+" ");}// Move to next lineSystem.out.println();}}publicstaticvoidmain(String[]args){intN=5;// Call the functionprintPattern(N);}}
Python
defprint_pattern(N):# Loop for rowsforiinrange(1,N+1):# Print numbers from 1 to iforjinrange(1,i+1):print(j,end=' ')# Move to next lineprint()if__name__=='__main__':N=5# Call the functionprint_pattern(N)
C#
usingSystem;classGfG{// Function to print the patternstaticvoidPrintPattern(intN){// Loop for rowsfor(inti=1;i<=N;i++){// Print numbers from 1 to ifor(intj=1;j<=i;j++){Console.Write(j+" ");}// Move to next lineConsole.WriteLine();}}staticvoidMain(){intN=5;// Call the functionPrintPattern(N);}}
JavaScript
functionprintPattern(N){// Loop for rowsfor(leti=1;i<=N;i++){// Print numbers from 1 to iletline='';for(letj=1;j<=i;j++){line+=j+' ';}// Move to next lineconsole.log(line);}}// Call the functionprintPattern(5);