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.
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 the use of a pattern program?
4.2.
What is the easiest method to solve a pattern problem?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Inverted V using Asterisk Pattern

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
Interview Puzzles

Introduction

In this article, we shall be covering Inverted V using an asterisk pattern. To understand this problem, one must understand the fundamental approach. 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 Asterisk Pattern.

Examples

Input : n = 6

Output : 

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

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

Input/Output:

Enter the row size:6
     *
    * *
   *   *
  *     *
 *       *
*         *

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

Input/Output:

Enter the row size:5
    *
   * *
  *   *
 *     *
*       *

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

Input/Output:

Enter the row size: 4
   *    
  * *   
 *   *  
*     * 

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

Input/Output:

Enter the row size:5
    *
   * *
  *   *
 *     *
*       *

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.

Previous article
Palindrome Pattern
Next article
Hollow Diamond Star Pattern
Live masterclass