Java Program to Print Flag Character Pattern

Program to Print Flag Character Pattern

In the previous article, we have discussed Java Program to Print V Character Pattern

In this article we are going to see how to print the flag character pattern.

Example-1

When rows value = 5

A
AB
ABC
ABCD
ABCDE
F
F
F
F
F

Now, let’s see the actual program to print it.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

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 once.
  • Take inner for loop to print the triangle.
  • Finally take a for loop to print the pole of the flag.

Java Code to Print Flag Character Pattern

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        // Starting ASCII value taken 65
        int ascii=65;
        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(r = 0; r<rows;r++)
            {
                //Inner loop that prints the triangle
                for(c = 0; c<=r;c++)
                    System.out.print((char)(r+ascii));
            //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((char)(r+ascii));
    }
}
Output:

Rows : 5

A 
AB 
ABC 
ABCD 
ABCDE 
F 
F 
F 
F 
F

C Code to Print Flag Character Pattern

#include <stdio.h>
int main(int argc, char const *argv[])
{
    // Starting ASCII value taken 65
    int ascii=65;
    printf("Rows : ");
    //Taking rows as input from user
    int rows, r, c;
    scanf("%d", &rows);
    
        //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("%c",(c+ascii));
            //Prints a new line
            printf("\n");
        }
    
       for (r = 0; r < rows; r++)
        //Another loop to print the pole of the flag
           printf("%c \n",(c+ascii));
    return 0;
}
Output:

Rows : 5

A
AB
ABC
ABCD
ABCDE
F 
F 
F 
F 
F

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Character Pattern Programs: