Program to Print Arrow Character Pattern
In the previous article, we have discussed Java Program to Print Hollow Square Character Pattern
In this article we are going to see how to print the arrow Character pattern.
Example: When row Size : 5 F EF DEF CDEF BCDEF ABCDEF BCDEF CDEF DEF EF F
Now, let’s see the actual program to print it.
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Approach:
- Enter size and store it in an integer variable
size
. - Take first for loop to print all rows.
- Take second/inner for loop to print column values.
- Then go on printing the characters according to the iteration.
Java Code to print Arrow Character Pattern
import java.util.Scanner; class Main { public static void main(String[] args) { int r, c,k; // starting ASCII value taken 65 int ascii=65; Scanner scan = new Scanner(System.in); System.out.print("Size : "); //Taking size as input from user int size=scan.nextInt(); //Outer for loop for (r = -size; r <= size; r++) { k = r; //To calculate the number of characters printed if (k < 0) { k *= -1; } //Inner Loop to print character and space for (c = 0; c <= size; c++) { if (c < k) //printing space System.out.print(" "); else //printing character System.out.print((char)(c+ascii)); } System.out.println(); } } }
Output: Size : 5 F EF DEF CDEF BCDEF ABCDEF BCDEF CDEF DEF EF F
C++ Code to print Arrow Character Pattern
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { int size, r, c, k; // starting ASCII value taken 65 int ascii=65; cout << "Size : "; //Taking size as input from user cin >> size; for (r = -size; r <= size; r++) { //inner loop k = r; if (k < 0) { //To calculate the number of stars printed k *= -1; } for (c = 0; c <= size; c++) { //Inner Loop to print star and space if (c < k) cout << " "; else cout << (char)(c+ascii); } //Prints a new line cout << endl; } return 0; }
Output: Size : 5 F EF DEF CDEF BCDEF ABCDEF BCDEF CDEF DEF EF 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: