×

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

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.

Advantages of Generics in Java

Generic offers a variety of benefits. The programmer's life is made easier by using generic Java. In this section, we are going to discuss about Java's generic’s and its benefits. 1....

4 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Diamond problem in Java

The Diamond Problem in Java is connected to multiple inheritances. It is also referred to as the "deadly diamond dilemma" or even the "deadly diamond of death”. The solution for...

5 minutes read.

How to generate random numbers in Java

Random numbers, also known as fake numbers, are actually a part of a very large sequence, so they are called random numbers. In a defined set of numbers, every number...

6 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

What is String in Java?

What is String in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a Java...

4 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

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...

2 minutes read.

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Addition Program in Java

Addition Program in Java We can perform the addition (sum) of two or more numbers by using the arithmetic operator (+). To write an addition program in Java, one must understand...

3 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.