Program to Print Flag Number Pattern
In the previous article, we have discussed Java Program to Print Zig Zag Number Pattern
In this article we are going to see how to print the flag number pattern.
Example-1 When rows value = 5 0 11 222 3333 44444 0 1 2 3 4
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 Number Pattern
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(r = 0; r<rows;r++)
{
//Inner loop that prints the triangle
for(c = 0; c<=r;c++)
System.out.print(r);
//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(r);
}
}
Output: Rows: 5 0 11 222 3333 44444 0 1 2 3 4
C Code to Print Flag Number 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("%d",r);
//Prints a new line
printf("\n");
}
for (r = 0; r < rows; r++)
//Another loop to print the pole of the flag
printf("%d \n",r);
return 0;
}
Output: Rows: 5 0 11 222 3333 44444 0 1 2 3 4
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 Number Pattern Programs: