Java Program To Print V Symbol Star Pattern
Program To Print V Symbol Star Pattern
In this article, we are going to see how to print the star pattern of the letter V.
Example-1When row value is 7
* *
* *
* *
* *
* *
* *
*Example-2 When row value is 5
* *
* *
* *
* *
*
Approach
- Enter total row and store it in an integer variable say
row
. - Take a inner for loop to print the column values.
- Keep iterating and print the values.
JAVA Code:
Method-1: Static Star Character
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int row;
// create scanner class to take user input
Scanner sc= new Scanner(System.in);
System.out.print("Enter no of row = ");
row=sc.nextInt();
int c, r;
int x = 1;
// store row*2-1 value in y
int y = row * 2 - 1;
// loop to iterate through rows
for (r = 1; r <= row; r++)
{
// iterate inner loop from 1 till row*2
for (c = 1; c <= row * 2; c++)
{
// if c= x or y print the symbol, else space
if (c == x || c == y)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
// increment x
x++;
// decrement y
y--;
System.out.println("");
}
}
}Output:Enter no of row = 5
* *
* *
* *
* *
*
You can Also Refer to other Star Pattern Programs:
Method-2: User Input Character
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int row;
// create scanner class to take user input
Scanner sc= new Scanner(System.in);
System.out.print("Enter no of row = ");
row=sc.nextInt();
System.out.print("Enter any character = ");
char s=sc.next().charAt(0);
int c, r;
int x = 1;
// store row*2-1 value in y
int y = row * 2 - 1;
// loop to iterate through rows
for (r = 1; r <= row; r++)
{
// iterate inner loop from 1 till row*2
for (c = 1; c <= row * 2; c++)
{
// if c= x or y print the symbol, else space
if (c == x || c == y)
{
System.out.print(s);
}
else
{
System.out.print(" ");
}
}
// increment x
x++;
// decrement y
y--;
System.out.println("");
}
}
}Output:Enter no of row = 5
Enter any character = #
# #
# #
# #
# #
#
Explanation:
Let’s understand the program with detailed explanation.
Let we have taken row as 5.
Iteration-I
r=1 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till row*2. It checks if c= x(i.e. now 1) or y (i.e. row*2–1). If condition satisfies print a symbol else a space. Total 2 stars and 7 spaces will be printed. Inner loop condition fails, so control come out of inner loop. x value incremented and y value decremented.
* *
Iteration-II
r=2 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till row*2. It checks if c= x(i.e. now 2) or y (i.e. now 8). If condition satisfies print a symbol else a space. Total 2 stars and 6 spaces will be printed. Inner loop condition fails, so control come out of inner loop. x value incremented and y value decremented.
* *
Iteration-III
r=3 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till row*2. It checks if c= x(i.e. now 3) or y (i.e. now 7). If condition satisfies print a symbol else a space. Total 2 stars and 5 spaces will be printed. Inner loop condition fails, so control come out of inner loop. x value incremented and y value decremented.
* *
Iteration-IV
r=4 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till row*2. It checks if c= x(i.e. now 4) or y (i.e. now 6). If condition satisfies print a symbol else a space. Total 2 stars and 4 spaces will be printed. Inner loop condition fails, so control come out of inner loop. x value incremented and y value decremented.
* *
Iteration-V
r=5 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till row*2. It checks if c= x(i.e. now 5) or y (i.e. now 5). If condition satisfies print a symbol else a space. Total 1 star and 4 spaces will be printed. Inner loop condition fails, so control come out of inner loop. x value incremented and y value decremented.
*
Now r=6, so first for loop condition fails i.e. r<=row. At no more for loop will be executed. At last we will see a pattern like this on the output screen.
* *
* *
* *
* *
*
C Code:
#include <stdio.h>
int main()
{
printf("Enter no of row = ");
int row;
scanf("%d", &row);
int c, r;
int x = 1;
int y = row * 2 - 1; for (r = 1; r <= row; r++)
{
for (c = 1; c <= row * 2; c++)
{
if (c == x || c == y)
{
printf("*");
}
else
{
printf(" ");
}
}
x++;
y--;
printf("\n");
}
}Output:Enter no of row = 5
* *
* *
* *
* *
*
C++ Code:
#include <iostream>
using namespace std;
int main()
{
cout<<"Enter no of row = ";
int row;
cin>>row;
int c, r;
int x = 1;
int y = row * 2 - 1; for (r = 1; r <= row; r++)
{
for (c = 1; c <= row * 2; c++)
{
if (c == x || c == y)
{
cout<<"*";
}
else
{
cout<<" ";
}
}
x++;
y--;
cout<<"\n";
}
}Output:Enter no of row = 5
* *
* *
* *
* *
*