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

You can also try this code with Online C Compiler
Run Code
Output
4
34
234
1234
01234
1234
234
34
4

You can also try this code with Online C Compiler
Run Code
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";
}
}

You can also try this code with Online C++ Compiler
Run Code
Output
5
45
345
2345
12345
012345
12345
2345
345
45
5

You can also try this code with Online C++ Compiler
Run Code
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!