×

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers.

We separate the entire procedure into the following steps that are required to print an Indian flag:

  • The flag's topmost layer.
  • The flag's second tier.
  • The flag's third layer.
  • The flagpole.
  • Stairs separate.

We only use a single nested for loop to print every section of the Indian flag.

Program to Print Flag Pattern in Java

import java.io.*;
import java.util.*;
// To print the Indian flag star pattern, build the class IndianFlag
public class IndianFlag
{  
    // start the main() method
    public static void main(String[] args)
 {  
        // declaring and setting up variables
        int height =  26;  
        int width = 40;  
        int temp = 3;  
        // To print the entire flag, we only require one nested loop.
for(int m = 1; m<= height; m++) {  
for(int n = 1; n <= width; n++){  
            // create the first part of the Indian flag  
if(m <= 4) {  
if(m == 1 || m == 4) {  
if(n >= 15 && n<= 35) {  
System.out.print("*");  
                        } else {  
System.out.print(" ");  
                        }  
                    } else {  
if(m ==2 || m ==3) {  
if(n == 15 || n == 35) {  
System.out.print("*");  
                            } else {  
System.out.print(" ");  
                            }  
                        }  
                    }  
                }  
if(m > 4 && m <= 8) {  
                    // second part of the flag  
if(m <= 7) {  
if(n == 15 || n == 35) {  
System.out.print("*");  
                        } else {  
if(n >= 22 && n<= 28) {  
System.out.print("-");  
                            } else {  
System.out.print(" ");  
                            }  
                        }  
                    } else {  
if(n >= 15 && n<= 35) {  
System.out.print("*");  
                        } else {  
System.out.print(" ");  
                        }  
                    }  
                }  
if(m > 8 && m <= 11) {  
                    // third part of the flag  
if(m <= 10) {  
if(n == 15 || n == 35) {  
System.out.print("*");  
                        } else {  
System.out.print(" ");  
                        }  
                    } else {  
if(n >= 15 && n <= 35) {  
System.out.print("*");  
                        } else {  
System.out.print(" ");  
                        }  
                    }  
                }  
if( m>= 12 && m <= 19) {  
                    // stick of the flag  
if(n == 15) {  
System.out.print("*");  
                    } else {  
System.out.print(" ");  
                    }  
                }  
                //  to print the stairs of the flag  
if( m>= 20) {  
if(m % 2 == 0) {  
if(n >= 15 - temp && n <= 15 + temp) {  
System.out.print("*");  
                        } else {  
System.out.print(" ");  
                        }  
                    } else {  
if(n == 15 - temp || n == 15 + temp) {  
System.out.print("*");  
                        } else {  
System.out.print(" ");  
                        }  
                    }  
                }  
            }  
if(m >= 20) {  
if(m % 2 != 0) {  
                    temp++;  
                }  
            }  
System.out.print("\n");       
        }  
    }  
}  

The output of the above program

Flag Pattern in Java

Related Topics

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Java Try Keyword

The try block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java try block has...

3 minutes read.

StringBuffer in Java

StringBuffer in Java Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe. Java StringBuffer ConstructorThe StringBuffer class has...

7 minutes read.

Java String Matches vs Contains

String Matches in Java The matches() function and its variations are used to determine whether or not a provided text matches a regular expression. The functioning as well as output of...

3 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Array and String based questions in Java

1. What is an Array in Java? A collection of identical data types is referred to as an array. There can be no separate data kinds. It supports the storage of...

4 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

Encapsulation Program in Java

Encapsulation Program in Java Encapsulation program in Java demonstrates the technique to bind methods and fields in a single unit. The term encapsulation is inspired by the word ‘capsule’, which is...

3 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.