Solution
Program in C
#include<stdio.h>
#include<math.h>
int main()
{
int output,input,a,c=1;
printf("Enter the size of the row:");
int rowSize;
scanf("%d",&rowSize);
for(output=rowSize;output>=-rowSize;output--)
{
for(input=1;input<=abs(output);input++)
{
printf(" ");
}
for(a=rowSize;a>=abs(output);a--)
{
printf("%d ",c);
}
if(output>0)
c++;
else
c--;
printf("\n");
}}

You can also try this code with Online C Compiler
Run Code
Input/Output:
Enter the size of the row:3
1
2 2
3 3 3
4 4 4 4
3 3 3
2 2
1
Program in C++
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int output,input,a,c=1;
cout<<"Enter the size of the row:";
int rowSize;
cin>>rowSize;
for(output=rowSize;output>=-rowSize;output--)
{
for(input=1;input<=abs(output);input++)
{
cout<<" ";
}
for(a=rowSize;a>=abs(output);a--)
{
cout<<c<<" ";
}
if(output>0)
c++;
else
c--;
cout<<"\n";
}}

You can also try this code with Online C++ Compiler
Run Code
Input/Output
Enter the size of the row:4
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Program in Java
import java.util.Scanner;
public class DiamondNumPattern {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int output,input,a,c=1;
System.out.println("Enter the size of the row:");
int rowSize=cs.nextInt();
for(output=rowSize;output>=-rowSize;output--)
{
for(input=1;input<=Math.abs(output);input++)
{
System.out.printf(" ");
}
for(a=rowSize;a>=Math.abs(output);a--)
{
System.out.print(c+" ");
}
if(output>0)
c++;
else
c--;
System.out.println();
}
cs.close();
}
}

You can also try this code with Online Java Compiler
Run Code
Input/Output
Enter the size of the row:
5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Program in Python
rowSize=int(input("Enter the size of the row:"))
c=1
for output in range(rowSize,-(rowSize+1),-1):
for inn in range(1,abs(output)+1):
print(" ",end="")
for a in range(rowSize,abs(output)-1,-1):
print(c,end=" ")
if output > 0:
c +=1
else:
c -=1
print("\r")

You can also try this code with Online Python Compiler
Run Code
Input/Output
Enter the size of the row:2
1
2 2
3 3 3
2 2
1
Frequently Asked Questions
What is Pattern Printing Program?
Pattern programs are patterns/designs/symbols made out of numbers, alphabets, or symbols in a specific order. Looping structures are used to solve these patterns.
How do you solve a pattern?
Step 1: Set up a table. The first step in looking for a pattern is to create a table that shows the existing relationships.
Step 2: Determine the Relationship Between Numbers.
Step 3: Draw some conclusions.
Step 4: Verify the Answer.
Also Read - Strong number in c
Conclusion
In this article, we have learned to design a diamond number pattern in C, C++, Java, and Python.
We hope that this blog has helped you enhance your knowledge of pattern problems in various programming languages.
Recommended Readings:
Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.
Also check out some of the Guided Paths on topics such as Interview Puzzles, Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.