Program to Print Left Arrow Star Pattern
In this article we are going to see how to print the left arrow star pattern
Example-1 When size value=4 **** *** ** * ** *** ****
Example-2: When size value=5 ***** **** *** ** * ** *** **** *****
Now, let’s see the actual program to print it.
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Approach:
- Enter the side of the square and store it in an integer variable
size. - Take one outer for loop for the upper half to iterate the rows.
- Take second/inner for loop to iterate the columns.
- Then go on printing the star symbols according to the iteration.
- Then take another outer for loop for the lower half and repeat the steps.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner;
class pattern
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Size : ");
//Taking size as input from user
int r, c, size=scan.nextInt();
for (r = 1; r <= size; ++r)
{ //Outer loop 1 for first half
for (c = 1; c <= size-r; ++c)
{//Inner for loop to print space
System.out.print(" ");
}
for (c = r; c <= size; ++c)
{//Inner for loop to print star
System.out.print("*");
}
//Prints a new line
System.out.println();
}
for (r = 1; r < size; ++r)
{ //Outer loop 2 for second half
for (c = 0; c < r; ++c)
{//Inner for loop to print space
System.out.print(" ");
}
for (c = 0; c <= r; ++c)
{//Inner for loop to print star
System.out.print("*");
}
//Prints a new line
System.out.println();
}
}
}
Output Size : 5 ***** **** *** ** * ** *** **** *****
Method-2 : User Input Character
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int r, c, size;
char ch;
Scanner scan = new Scanner(System.in);
System.out.print("Size : ");
//Taking size as input from user
size=scan.nextInt();
System.out.print("Character : ");
//Taking random character as input from user
ch=scan.next().charAt(0);
for (r = 1; r <= size; ++r)
{ //Outer loop 1 for first half
for (c = 1; c <= size-r; ++c)
{//Inner for loop to print space
System.out.print(" ");
}
for (c = r; c <= size; ++c)
{//Inner for loop to print star
System.out.print(ch);
}
//Prints a new line
System.out.println();
}
for (r = 1; r < size; ++r)
{ //Outer loop 2 for second half
for (c = 0; c < r; ++c)
{//Inner for loop to print space
System.out.print(" ");
}
for (c = 0; c <= r; ++c)
{//Inner for loop to print star
System.out.print(ch);
}
//Prints a new line
System.out.println();
}
}
}
Output Size : 5 Character : ~ ~~~~~ ~~~~ ~~~ ~~ ~ ~~ ~~~ ~~~~ ~~~~~
C Code:
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Size : ");
//Taking size as input from user
int size, r, c;
scanf("%d", &size);
for (r = 1; r <= size; ++r)
{ //Outer loop 1 for first half
for (c = 1; c <= size - r; ++c)
{ //Inner for loop to print space
printf(" ");
}
for (c = r; c <= size; ++c)
{ //Inner for loop to print star
printf("*");
}
//Prints a new line
printf("\n");
}
for (r = 1; r < size; ++r)
{ //Outer loop 2 for second half
for (c = 0; c < r; ++c)
{ //Inner for loop to print space
printf(" ");
}
for (c = 0; c <= r; ++c)
{ //Inner for loop to print star
printf("*");
}
//Prints a new line
printf("\n");
}
return 0;
}
Output Size : 5 ***** **** *** ** * ** *** **** *****
C++ Code:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
cout << "Size : ";
//Taking size as input from user
int size, r, c;
cin >> size;
for (r = 1; r <= size; ++r)
{ //Outer loop 1 for first half
for (c = 1; c <= size - r; ++c)
{ //Inner for loop to print space
cout << " ";
}
for (c = r; c <= size; ++c)
{ //Inner for loop to print star
cout << "*";
}
//Prints a new line
cout << endl;
}
for (r = 1; r < size; ++r)
{ //Outer loop 2 for second half
for (c = 0; c < r; ++c)
{ //Inner for loop to print space
cout << " ";
}
for (c = 0; c <= r; ++c)
{ //Inner for loop to print star
cout << "*";
}
//Prints a new line
cout << endl;
}
return 0;
}
Output: Size : 5 ***** **** *** ** * ** *** **** *****
Related Java Star Pattern Programs: