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.
3.2.
C++
3.3.
Python 
4.
Frequently Asked Questions
4.1.
What is the difference between a do-while loop and a while loop 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

V-Shape Number 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 the V Shape Number Pattern where the number of rows ‘N’ is taken as the input, and the pattern will be printed as the output.  

Now let us understand the statement with the help of an example.


Example

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

Output :

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

Approach

We could solve this problem using the two loops i and j. The outer for-loop ‘i’ depicts the number of rows for the pattern, whereas the inner for-loop is for the number of columns.

#include <stdio.h>
int main()
{
  int i,j;
  //Entering row size
  int row_size;
  row_size = 7;
  int x = 1;
  int y = row_size * 2 - 1;
  for (i = row_size; i >= 1; i--)
  {
    for (j = 1; j <= row_size * 2; j++)
    {
      if (j == x || j == y)
      {
        printf("%d", i);
      }
      else
      {
        printf(" ");
      }
    }
    x++;
    y--;
    printf("\n");
  }
}

 

Output

7           7 
 6         6  
  5       5   
   4     4    
    3   3     
     2 2      
      1       


C++

#include <iostream>
using namespace std;
int main()
{
      int i, j, p;
      cout << "Enter the row size:";
      int n;
      n=6;
      cout<<"Row size entered is ” <<n;
      int x = 1;
      int y = n * 2 - 1;
      for (i = n; i >= 1; i--)
      {
          for (j = 1; j <= n * 2; j++)
          {
              if (j == x || j == y)
              {
                  cout << i;
              }
              else
              {
                  cout << " ";
              }
          }
          x++;
          y--;
          cout << "\n";
      }
}

 

Output

Row size entered is 6
6         6 
 5       5  
  4     4   
   3   3    
    2 2     
     1

 

Python 

row_size = 4
x = 1
y = row_size*2-1
for i in range(row_size, 0, -1):
    for j in range(1, row_size*2+1):
        if j == x or j == y:
            print(i, end="")
        else:
            print(" ", end="")
    x += 1
    y -= 1
    print("\r")

 

Output

4        4 

 3      3  

  2   2   

    1    

Frequently Asked Questions

What is the difference between a do-while loop and a while loop in C++?

The significant distinction between a while loop and a do-while loop is that a while loop checks for conditions before loop iteration. On the other hand, the do-while loop tests the condition after the statements inside the loop have been executed. The while loop is also known as an entry-controlled loop. On the other hand, the exit controlled loop is known as the do-while loop.

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

The do-while loop evaluates the condition at the ending of the loop, which 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 V-shape Number patterns 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 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.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Previous article
Inverted V-shape Number Pattern
Next article
Half Diamond Number Pattern
Live masterclass