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.
Approach
3.1.
C Code
3.2.
C++ Code
3.3.
Complexity Analysis
4.
Frequently Asked Questions
4.1.
What is the difference between for loops and while loops in C++?
4.2.
What are do-while loops in C++? When do we use do-while loops?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Inverted Half Diamond Pattern

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

Introduction

Puzzles are good exercise for the brain. They help in enhancing the cognitive abilities of the brain helping with Problem Solving and related skills. There are numerous types of puzzles; each one having a logic inherent to itself which helps in cracking it. A good puzzle well is actually like a good mystery that we may have read about or watched on TV. It has the finest of hints which help in reaching its solution.

The following article discusses one such puzzle so let's get right to it.

Problem Statement

Write a program to print a Solid Half Diamond Number Pattern where the number of rows ‘N’ is taken as the input, and the pattern will be printed as the output.  

Let's understand the statement with the help of an example

Example

Input : Enter the row size:4  // n is the row size

Output :

        4
       34
      234
     1234
    01234
     1234
      234
       34
        4
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

Approach

We could solve this problem using the two loops i and j. These loops keep track of the rows and columns of the pattern; on the other hand, the for-loop for variable ‘p’  prints the output of the numbers in a half inverted diamond pattern.

C Code

#include<stdio.h>
#include<math.h>
int main()
{
    printf("Enter the row size:");
    int row_size;
    row_size = 4;
    int j,i,p;
    for(i=row_size;i>=-row_size;i--)
     {
         for(j=1;j<=abs(i);j++)
                printf(" ");
            for(p=abs(i);p<=row_size;p++)
                printf("%d",p);
            printf("\n");
     }
}

Output

        4
       34
      234
     1234
    01234
     1234
      234
       34
        4

 

C++ Code

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n;
    n=5;
    int j,i,p;
    for(i=n ;i >= (-n); i--)
     {
         for(j=1 ; j <= abs(i) ; j++)
             cout<<" ";
            for(p=abs(i) ; p<=n ; p++)
                cout<<p;
            cout<<"\n";
     }
}

Output

         5
        45
       345
      2345
     12345
    012345
     12345
      2345
       345
        45
         5

Complexity Analysis

  • Time Complexity: O(N2)
  • Space Complexity: O(N2)

Frequently Asked Questions

What is the difference between for loops and while loops in C++?

Difference between for loop and while loop is that number of iterations to be performed is already known and is used to get a specific result in a for loop. Still, the command runs until a particular condition is met and the assertion is proven untrue in a while loop.

What are do-while loops in C++? When do we use do-while loops?

At the ending of the loop, the do-while loop evaluates the condition. This implies that even if the condition is never true, the statements inside the loop body will be executed at least once. The do-while loop is primarily useful when the loop must be executed at least once. It is commonly employed in menu-driven systems where the end-user determines the termination condition.

Conclusion

In this article, we have extensively discussed the program to print inverted half-diamond pattern using numbers. We tried to cover it with some examples.

You can look at more exciting blogs and articles curated by the coding ninja's team by visiting Library.

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 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.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Previous article
Half Diamond Number Pattern
Next article
Hollow Diamond Number Pattern
Live masterclass