Program to Print Right Arrow Star Pattern
In this article we are going to see how to print the right 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 = 0; r < size; ++r) { //Outer loop 1 for first half for (c = 0; c < size; ++c) if(c<r) //To print space System.out.print(" "); //Two spaces else //To print star System.out.print("*"); //Prints a new line System.out.println(); } for (r = 2; r <= size; ++r) { //Outer loop 2 for second half for (c = 0; c < size; ++c) //To print space if(c<size-r) System.out.print(" "); //Two spaces else //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 sym; 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 sym=scan.next().charAt(0); for (r = 0; r < size; ++r) { //Outer loop 1 for first half for (c = 0; c < size; ++c) if(c<r) //To print space System.out.print(" "); //Two spaces else //To print star System.out.print(sym); //Prints a new line System.out.println(); } for (r = 2; r <= size; ++r) { //Outer loop 2 for second half for (c = 0; c < size; ++c) //To print space if(c<size-r) System.out.print(" "); //Two spaces else //To print star System.out.print("*"); //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 = 0; r < size; ++r) { //Outer loop 1 for first half for (c = 0; c < size; ++c) if (c < r) //To print space printf(" "); //Two spaces else //To print star printf("*"); //Prints a new line printf("\n"); } for (r = 2; r <= size; ++r) { //Outer loop 2 for second half for (c = 0; c < size; ++c) //To print space if (c < size - r) printf(" "); //Two spaces else //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 = 0; r < size; ++r) { //Outer loop 1 for first half for (c = 0; c < size; ++c) if (c < r) //To print space cout << " "; //Two spaces else //To print star cout << "*"; //Prints a new line cout << endl; } for (r = 2; r <= size; ++r) { //Outer loop 2 for second half for (c = 0; c < size; ++c) //To print space if (c < size - r) //To print space cout << " "; //Two spaces else //To print star cout << "*"; //Prints a new line cout << endl; } return 0; }
Output Size : 5 ***** **** *** ** * ** *** **** *****
Related Java Star Pattern Programs: