Solution
Program in C++
The source code for the C++ program that prints the Inverted V Star Pattern may be found here.
#include <iostream>
using namespace std;
int main()
{
cout<<"Enter the row size:";
int row_size,out,in;
cin>>row_size;
int print_control_x=row_size;
int print_control_y=row_size;
for(out=1;out<=row_size;out++)
{
for(in=1;in<=row_size*2;in++)
{
if(in==print_control_x||in==print_control_y)
{
cout<<"*";
}
else
{
cout<<" ";
}
}
print_control_x--;
print_control_y++;
cout<<"\n";
}
}

You can also try this code with Online C Compiler
Run Code
Input/Output:
Enter the row size:6
*
* *
* *
* *
* *
* *

You can also try this code with Online C Compiler
Run Code
Program in C
The source code for the C program that prints the Inverted V Star Number Pattern may be found here.
#include <stdio.h>
int main()
{
printf("Enter the row size:");
int row_size,out,in;
scanf("%d",&row_size);
int print_control_x=row_size;
int print_control_y=row_size;
for(out=1;out<=row_size;out++)
{
for(in=1;in<=row_size*2;in++)
{
if(in==print_control_x||in==print_control_y)
{
printf("*");
}
else
{
printf(" ");
}
}
print_control_x--;
print_control_y++;
printf("\n");
}
}

You can also try this code with Online C Compiler
Run Code
Input/Output:
Enter the row size:5
*
* *
* *
* *
* *

You can also try this code with Online C Compiler
Run Code
Program in Java
Here is the Java source code for printing the Inverted V Star Pattern.
import java.util.Scanner;
public class P36 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
System.out.println("Enter the row size:");
int out,in;
int row_size=cs.nextInt();
int print_control_x=row_size;
int print_control_y=row_size;
for(out=1;out<=row_size;out++)
{
for(in=1;in<=row_size*2;in++)
{
if(in==print_control_x||in==print_control_y)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
print_control_x--;
print_control_y++;
System.out.println();
}
cs.close();
}
}

You can also try this code with Online C Compiler
Run Code
Input/Output:
Enter the row size: 4
*
* *
* *
* *

You can also try this code with Online C Compiler
Run Code
Program in Python
Here is the Python source code for printing the Inverted V Star Pattern.
row_size=int(input("Enter the row size:"))
print_control_x=row_size
print_control_y=row_size
for out in range(1,row_size+1):
for in1 in range(1,row_size*2+1):
if in1==print_control_x or in1==print_control_y:
print("*",end="")
else:
print(" ", end="")
print_control_x-=1
print_control_y+=1
print("\r")

You can also try this code with Online C Compiler
Run Code
Input/Output:
Enter the row size:5
*
* *
* *
* *
* *

You can also try this code with Online C Compiler
Run Code
In all the above examples, the output will be a pattern in the form of an Inverted V.
Input Format
The first line contains the row size of the pattern, while the other variables were for the internal and external of the pattern.
Output Format
We are printing the pattern in the form of an Inverted V.
Frequently Asked Questions
What is the use of a pattern program?
Pattern programs are the best way to study the loop structure in general programming. It enables us to visualize how the iteration works in a loop.
What is the easiest method to solve a pattern problem?
First, we need to make a table to look for patterns, find the relationship between the patterns, make a prediction and then check the answer.
Conclusion
In this article, we have learned the Inverted V using an asterisk pattern using the stars. We also explained it is working and how it will go through step by step. We also took the example and the output of the pattern.
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.