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 a pattern program?
4.2.
Are patterns asked in interviews?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Inverted V-shape Number Pattern

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
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 Inverted V Number Pattern.

Examples

Input : n = 5

Output : 

     1     
   2 2    
  3   3   
 4     4  
5       5 

Input : n = 6
Output : 

      1
     2 2
    3   3
   4     4
  5       5
 6         6

Solution

Program in C

#include <stdio.h>
int main()
{
  int output, input;
  printf("Enter the size of the row:");
  int rowSize;
  scanf("%d", &rowSize);
  int x = rowSize;
  int y = rowSize;
  for (output = 1; output <= rowSize; output++)
  {
    for (input = 1; input <= rowSize * 2; input++)
    {
      if (input == x || input == y)
      {
        printf("%d", output);
      }
      else
      {
        printf(" ");
      }
    }
    x--;
    y++;
    printf("\n");
  }
}
You can also try this code with Online C Compiler
Run Code

Input/Output:

Enter the size of the row:2
 1
2 2

Program in C++

#include <iostream>
using namespace std;
int main()
{
    int output, input, c;
    cout << "Enter size of the row:";
    int rowSize;
    cin >> rowSize;


    int x = rowSize;
    int y = rowSize;
    for (output = 1; output <= rowSize; output++)
    {
        for (input = 1; input <= rowSize * 2; input++)
        {
            if (input == x || input == y)
            {
                cout << output;
            }
            else
            {
                cout << " ";
            }
        }
        x--;
        y++;
        cout << "\n";
    }
}
You can also try this code with Online C++ Compiler
Run Code

Input/Output

Enter size of the row:3
  1
 2 2
3   3

Program in Java

import java.util.Scanner;
public class InvertedVNumPattern {
    public static void main(String[] args) {
        Scanner cs = new Scanner(System.in);
        System.out.println("Enter the size of the row:");
        int output, input;
        int rowSize = cs.nextInt();
        int x = rowSize;
        int y = rowSize;
        for (output = 1; output <= rowSize; output++) {
            for (input = 1; input <= rowSize * 2; input++) {
                if (input == x || input == y) {
                    System.out.print(output);
                } else {
                    System.out.printf(" ");
                }
            }
            x--;
            y++;
            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:
4
   1
  2 2
 3   3
4     4

Program in Python

rowSize = int(input("Enter the size of the row:"))
x = rowSize
y = rowSize
for output in range(1, rowSize+1):
    for inn in range(1, rowSize*2):
        if inn == x or inn == y:
            print(output, end="")
        else:
            print(" ", end="")
    x -= 1
    y += 1
    print("\r")
You can also try this code with Online Python Compiler
Run Code

Input/Output

Enter the size of the row:5
    1
   2 2
  3   3
 4     4
5       5

Frequently Asked Questions

What is a pattern program?

Pattern programs are nothing more than patterns of numbers, alphabets, or symbols in a particular order. These kinds of pattern programs are simply solved utilizing the for loop condition.

Are patterns asked in interviews?

During coding interviews, applicants are frequently tested using a pattern program.

Conclusion

In this article, we have learned to design an Inverted V-shape Number pattern in different programming languages.

We hope that this blog has helped you enhance your knowledge of pattern problems in various programming languages. 
To practice more such interview problems and upskill yourself, check out our 

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.

Live masterclass