Program to Print the V Star Pattern

*       * 
 *     *  
  *   *   
   * *    
    *  

Problem Statement:- Program to Print the V shape Star Pattern.


 Data requirement:-

   Input Data:- row_size

  Output Data:- *

 Additional Data:-out, start_print, in or inn(for python), print_control_x,  print_control_y

Program in C

Here is the source code of the C Program to Print the V Star Pattern.


Code:

#include <stdio.h>
int main()
{
  printf("Enter the row size:");
  int row_size;
  scanf("%d", &row_size);
  int inoutp;
  int print_control_x = 1;
  int print_control_y = row_size * 2 - 1;

  for (out = 1out <= row_sizeout++)
  {
    for (in = 1in <= row_size * 2in++)
    {
      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 C++

Here is the source code of the C++ Program to Print the V shape Star Pattern.


Code:

#include <iostream>
using namespace std;
int main()
{
    cout << "Enter the row size:";
    int row_size;
    cin >> row_size;
    int inoutp;
    int print_control_x = 1;
    int print_control_y = row_size * 2 - 1;

    for (out = 1out <= row_sizeout++)
    {
        for (in = 1in <= row_size * 2in++)
        {
            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:4
*     *
 *   *
  * *
   *

Program in Java
  

Here is the source code of the Java  Program to Print the V-shaped Star Pattern.


Code:

import java.util.Scanner;

public class StarPattern12 {

    public static void main(String[] args) {
        Scanner cs = new Scanner(System.in);
        System.out.println("Enter the row size:");
        int outin;
        int row_size = cs.nextInt();
        int print_control_x = 1;
        int print_control_y = row_size * 2 - 1;
        for (out = 1out <= row_sizeout++) {
            for (in = 1in <= row_size * 2in++) {
                if (in == print_control_x || in == print_control_y) {
                    System.out.printf("*");
                } else {
                    System.out.printf(" ");
                }
            }
            print_control_x++;
            print_control_y--;
            System.out.println();
        }
        cs.close();
    }
}


Input/Output:
Enter the row size:
6
*         * 
 *       *  
  *     *   
   *   *    
    * *     
     *      

Program in Python
  
Here is the source code of the  Program to Print the V shape Star Pattern.

Code:

row_size = int(input("Enter the row size:"))
print_control_x = 1
print_control_y = row_size*2-1
for out in range(1row_size+1):
    for inn in range(1row_size*2+1):
        if inn == print_control_x or inn == print_control_y:
            print("*"end="")
        else:
            print(" "end="")
    print_control_x += 1
    print_control_y -= 1
    print("\r")


Input/Output:
Enter the row size:4
*     * 
 *   *  
  * *   
   *    


More:-

Post a Comment

0 Comments