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.