Program to Print Flag Star Pattern
In this article we are going to see how to print the flag star pattern
Example-1 When rows value = 7 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Example-2 When rows value = 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Now, let’s see the actual program to print it.
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Approach:
- Enter the number of rows to print and store it in an integer variable
rows
. - Take an outer for loop to print the triangle twice.
- Take inner for loop to print the triangle.
- Finally take a for loop to print the pole of the flag.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Rows : "); //Taking total rows as input from user int r, c, rows = scan.nextInt(); //Outer loop to print the top triangle two times for(int p = 0;p<2;p++) { //Inner loop that prints the triangle for(r = 0; r<rows;r++) { for(c = 0; c<=r;c++) System.out.print("* "); //Prints a new line System.out.println(); } } for(r = 0; r<rows;r++) //Another loop to print the pole of the flag System.out.println("*"); } }
Output- Rows : 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Method-2 : User Input Character
import java.util.Scanner; class Main { public static void main(String[] args) { int r, c, rows; char k; Scanner scan = new Scanner(System.in); //Taking total rows as input from user System.out.print("Rows : "); rows = scan.nextInt(); //Taking any random character as input from user System.out.print("Character : "); k = scan.next().charAt(0); //Outer loop to print the top triangle two times for(int p = 0;p<2;p++) { //Inner loop that prints the triangle for(r = 0; r<rows;r++) { for(c = 0; c<=r;c++) System.out.print(k+" "); //Prints a new line System.out.println(); } } for(r = 0; r<rows;r++) //Another loop to print the pole of the flag System.out.println(k); } }
Output- Rows : 5 Character : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
C Code:
#include <stdio.h> int main(int argc, char const *argv[]) { printf("Rows : "); //Taking rows as input from user int rows, r, c; scanf("%d", &rows); for (int p = 0; p < 2; p++) { //Outer loop to print the top triangle two times for (r = 0; r < rows; r++) { //Inner loop that prints the triangle for (c = 0; c <= r; c++) printf("* "); //Prints a new line printf("\n"); } } for (r = 0; r < rows; r++) //Another loop to print the pole of the flag printf("*\n"); return 0; }
Output- Rows : 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
C++ Code:
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { cout << "Rows : "; //Taking rows as input from user int rows, r, c; cin >> rows; for (int p = 0; p < 2; p++) { //Outer loop to print the top triangle two times for (r = 0; r < rows; r++) { //Inner loop that prints the triangle for (c = 0; c <= r; c++) cout << "* "; //Prints a new line cout << endl; } } for (r = 0; r < rows; r++) //Another loop to print the pole of the flag cout << "* " << endl; return 0; }
Output- Rows : 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Related Java Star Pattern Programs: