Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
3.
Solution
3.1.
Program in C
3.2.
Program in C++
3.3.
Program in Java
3.4.
Program in Python
4.
Frequently Asked Questions
4.1.
What is Pattern Printing Program?
4.2.
How do you solve a pattern?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Diamond Number Pattern

Author Sanjana Yadav
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
Interview Puzzles

Introduction

As the name implies, a pattern is a repeated sequence that consistently repeats itself. A pattern is a general solution that may be used repeatedly to a recurring problem.

To answer any random pattern question, it is necessary to comprehend the fundamentals and acquire the techniques. It may look mysterious and complex at first, but patterns are not that complicated since it gives a straightforward technique to recognize and code to produce a suitable and systematically created pattern.

Problem Statement

Write a program to produce a diamond shape with 2n rows given the integer n.

Examples

First Sample Input: 5

First Sample Output: 

     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

Second Sample Input: 4

Second Sample Output: 


    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
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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");
}}

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";
}}

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();
    }
}

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")

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.

Previous article
Butterfly Shape Pattern
Next article
Inverted V-shape Number Pattern
Live masterclass