The diamond pattern can be generated by printing two triangular patterns. The first part prints the upper half where the number of stars increases in each row while the spaces decrease. The second part prints the lower half where the number of stars decreases and the spaces increase. Nested loops are used to print spaces and stars in each row.
Step-by-Step Approach:
Initialize a variable space = n - 1 to track the number of spaces before stars.
Run a loop from i = 0 to n - 1 to print the upper half of the diamond.
Print space number of spaces using a loop.
Print i + 1 stars separated by spaces.
Decrease space after each row.
Reset space = 0 for the lower half of the diamond.
Run another loop from i = n down to 1.
Print space number of spaces.
Print i stars separated by spaces.
Increase space after each row.
C++
//Driver Code Startsusingnamespacestd;// Prints diamond pattern with 2n rows //Driver Code EndsvoidprintDiamond(intn){intspace=n-1;// run loop (parent loop) // till number of rows for(inti=0;i<n;i++){// loop for initially space, // before star printing for(intj=0;j<space;j++)cout<<" ";// Print i+1 stars for(intj=0;j<=i;j++)cout<<"* ";cout<<endl;space--;}// Repeat again in reverse order space=0;// run loop (parent loop) // till number of rows for(inti=n;i>0;i--){// loop for initially space, // before star printing for(intj=0;j<space;j++)cout<<" ";// Print i stars for(intj=0;j<i;j++)cout<<"* ";cout<<endl;space++;}}//Driver Code Startsintmain(){printDiamond(5);return0;}//Driver Code Ends
C
//Driver Code Starts#include<stdio.h>// Prints diamond // pattern with 2n rows//Driver Code EndsvoidprintDiamond(intn){intspace=n-1;// run loop (parent loop)// till number of rowsfor(inti=0;i<n;i++){// loop for initially space, // before star printingfor(intj=0;j<space;j++)printf(" ");// Print i+1 starsfor(intj=0;j<=i;j++)printf("* ");printf("");space--;}// Repeat again in // reverse orderspace=0;// run loop (parent loop)// till number of rowsfor(inti=n;i>0;i--){// loop for initially space, // before star printingfor(intj=0;j<space;j++)printf(" ");// Print i starsfor(intj=0;j<i;j++)printf("* ");printf("");space++;}}//Driver Code Startsintmain(){printDiamond(5);return0;}//Driver Code Ends
Java
//Driver Code StartsclassGFG{// Prints diamond pattern// with 2n rows//Driver Code EndsstaticvoidprintDiamond(intn){intspace=n-1;// run loop (parent loop)// till number of rowsfor(inti=0;i<n;i++){// loop for initially space,// before star printingfor(intj=0;j<space;j++)System.out.print(" ");// Print i+1 starsfor(intj=0;j<=i;j++)System.out.print("* ");System.out.print("
");space--;}// Repeat again in// reverse orderspace=0;// run loop (parent loop)// till number of rowsfor(inti=n;i>0;i--){// loop for initially space,// before star printingfor(intj=0;j<space;j++)System.out.print(" ");// Print i starsfor(intj=0;j<i;j++)System.out.print("* ");System.out.print("
");space++;}}//Driver Code Startspublicstaticvoidmain(String[]args){printDiamond(5);}}//Driver Code Ends
Python
# Prints diamond pattern with 2n rowsdefprintdiamond(n):space=n-1# run loop (parent loop)# till number of rowsforiinrange(0,n):# loop for initially space,# before star printingforjinrange(0,space):print(" ",end="")# Print i+1 starsforjinrange(0,i+1):print("* ",end="")print()space-=1# Repeat again in reverse orderspace=0# run loop (parent loop)# till number of rowsforiinrange(n,0,-1):# loop for initially space,# before star printingforjinrange(0,space):print(" ",end="")# Print i starsforjinrange(0,i):print("* ",end="")print()space+=1#Driver Code Startsdefmain():printdiamond(5)if__name__=="__main__":main()#Driver Code Ends
C#
//Driver Code StartsusingSystem;classGFG{// Prints diamond pattern// with 2n rows//Driver Code EndsstaticvoidprintDiamond(intn){intspace=n-1;// run loop (parent loop) // till number of rowsfor(inti=0;i<n;i++){// loop for initially space,// before star printingfor(intj=0;j<space;j++)Console.Write(" ");// Print i+1 starsfor(intj=0;j<=i;j++)Console.Write("* ");Console.Write("
");space--;}// Repeat again in// reverse orderspace=0;// run loop (parent loop)// till number of rowsfor(inti=n;i>0;i--){// loop for initially space, // before star printingfor(intj=0;j<space;j++)Console.Write(" ");// Print i starsfor(intj=0;j<i;j++)Console.Write("* ");Console.Write("
");space++;}}//Driver Code StartspublicstaticvoidMain(){printDiamond(5);}}//Driver Code Ends
JavaScript
//Driver Code Starts// Prints diamond pattern with 2n rows//Driver Code Endsfunctionprintdiamond(n){letspace=n-1;// run loop (parent loop)// till number of rowsfor(leti=0;i<n;i++){// loop for initially space,// before star printingfor(letj=0;j<space;j++)process.stdout.write(" ");// Print i+1 starsfor(letj=0;j<=i;j++)process.stdout.write("* ");console.log();space--;}// Repeat again in reverse orderspace=0;// run loop (parent loop)// till number of rowsfor(leti=n;i>0;i--){// loop for initially space,// before star printingfor(letj=0;j<space;j++)process.stdout.write(" ");// Print i starsfor(letj=0;j<i;j++)process.stdout.write("* ");console.log();space++;}}//Driver Code Starts// driver codeprintdiamond(5);//Driver Code Ends
In this approach, recursion is used to print spaces and stars for each row of the diamond. Separate recursive functions print blank spaces, stars, the upper half, and the lower half of the diamond.
C++
//Driver Code Startsusingnamespacestd;//Driver Code Ends// Function to print stars in a line of the diamondvoidgotonextLine(intk,inti,intz){// base caseif(k==i)return;cout<<"* ";gotonextLine(k+z,i,z);}voidaddblankSpaceInDiamond(// print blank space of diamondintj,inti,intz){if(j==i)return;cout<<" ";addblankSpaceInDiamond(j+z,i,z);}voidupperDiamond(introw,inti){// base caseif(i>row)return;addblankSpaceInDiamond(row,i,-1);gotonextLine(0,i,1);cout<<endl;// recursive callupperDiamond(row,i+1);}voidlowerDiamond(introw,inti){// base caseif(i>row)return;addblankSpaceInDiamond(0,i,1);gotonextLine(row,i,-1);cout<<endl;lowerDiamond(row,i+1);}//Driver Code Startsintmain(){introw;row=5;// print upper part of triangleupperDiamond(row,0);// print lower part of diamondlowerDiamond(row,0);return0;}//Driver Code Ends
Java
//Driver Code StartsclassGFG{//Driver Code Ends// Function to print stars in a line of the diamondstaticvoidgotonextLine(intk,inti,intz){// base caseif(k==i)return;System.out.print("* ");gotonextLine(k+z,i,z);}// print blank space of diamondstaticvoidaddblankSpaceInDiamond(intj,inti,intz){if(j==i)return;System.out.print(" ");addblankSpaceInDiamond(j+z,i,z);}staticvoidupperDiamond(introw,inti){// base caseif(i>row)return;addblankSpaceInDiamond(row,i,-1);gotonextLine(0,i,1);System.out.println();// recursive callupperDiamond(row,i+1);}staticvoidlowerDiamond(introw,inti){// base caseif(i>row)return;addblankSpaceInDiamond(0,i,1);gotonextLine(row,i,-1);System.out.println();lowerDiamond(row,i+1);}//Driver Code Startspublicstaticvoidmain(String[]args){introw;row=5;// print upper part of triangleupperDiamond(row,0);// print lower part of diamondlowerDiamond(row,0);}}//Driver Code Ends
Python
# Function to print stars in a line of the diamonddefgotonextline(k,i,z):# base caseifk==i:returnprint("*",end=" ")gotonextline(k+z,i,z)# print blank space of diamonddefaddblankspaceindiamond(j,i,z):ifj==i:returnprint(" ",end="")addblankspaceindiamond(j+z,i,z)defupperdiamond(row,i):# base caseifi>row:returnaddblankspaceindiamond(row,i,-1)gotonextline(0,i,1)print()# recursive callupperdiamond(row,i+1)deflowerdiamond(row,i):# base caseifi>row:returnaddblankspaceindiamond(0,i,1)gotonextline(row,i,-1)print()lowerdiamond(row,i+1)defmain():#Driver Code Startsrow=5# print upper part of triangleupperdiamond(row,0)# print lower part of diamondlowerdiamond(row,0)if__name__=="__main__":main()#Driver Code Ends
C#
//Driver Code StartsusingSystem;//Driver Code EndsclassGFG{// Function to print stars in a line of the diamondstaticvoidgotonextLine(intk,inti,intz){// base caseif(k==i)return;Console.Write("* ");gotonextLine(k+z,i,z);}// print blank space of diamondstaticvoidaddblankSpaceInDiamond(intj,inti,intz){if(j==i)return;Console.Write(" ");addblankSpaceInDiamond(j+z,i,z);}staticvoidupperDiamond(introw,inti){// base caseif(i>row)return;addblankSpaceInDiamond(row,i,-1);gotonextLine(0,i,1);Console.WriteLine();// recursive callupperDiamond(row,i+1);}staticvoidlowerDiamond(introw,inti){// base caseif(i>row)return;addblankSpaceInDiamond(0,i,1);gotonextLine(row,i,-1);Console.WriteLine();lowerDiamond(row,i+1);}//Driver Code StartsstaticvoidMain(){introw=5;// print upper part of triangleupperDiamond(row,0);// print lower part of diamondlowerDiamond(row,0);}}//Driver Code Ends
JavaScript
//Driver Code Starts"use strict";// Function to print stars in a line of the diamond//Driver Code EndsfunctiongotonextLine(k,i,z){// base caseif(k===i)return;process.stdout.write("* ");gotonextLine(k+z,i,z);}// Function to print blank spacesfunctionaddblankSpaceInDiamond(j,i,z){if(j===i)return;process.stdout.write(" ");addblankSpaceInDiamond(j+z,i,z);}// Function to print upper half of diamondfunctionupperDiamond(row,i){// base caseif(i>row)return;addblankSpaceInDiamond(row,i,-1);gotonextLine(0,i,1);console.log();// recursive callupperDiamond(row,i+1);}// Function to print lower half of diamondfunctionlowerDiamond(row,i){// base caseif(i>row)return;addblankSpaceInDiamond(0,i,1);gotonextLine(row,i,-1);console.log();lowerDiamond(row,i+1);}//Driver Code Starts// driver codeconstrow=5;// print upper part of triangleupperDiamond(row,0);// print lower part of diamondlowerDiamond(row,0);//Driver Code Ends