Program to Print Inverted V Character Pattern
Inverted v symbol: In the previous article, we have discussed Java Program to Print Hollow Circle Character Pattern
In this article we are going to see how to print the inverted V character pattern.
- Java Code to Print Inverted V Character Pattern
- C Code to Print Inverted V Character Pattern
- C++ Code to Print Inverted V Character Pattern
Example-1 When rows value = 5 E D D C C B B A A
Example-2 When rows value = 6 H G G F F E E D D C C B B A A
Now, let’s see the actual program to print it.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Approach:
- Enter the number of rows to print and store it in an integer variable
rows
. - Take first for loop to print all rows.
- Take inner for loop to print column values and one to print empty spaces.
- Then go on printing the characters according to the iteration.
Java Code to Print Inverted V Character Pattern
import java.util.Scanner; public class Main { public static void main(String[] args) { int r,s=0, c, rows; //Starting ASCII value taken 64 int ascii=65; Scanner scan = new Scanner(System.in); System.out.print("Enter Rows : "); //Taking total rows as input from user rows = scan.nextInt(); for(r = rows-1; r>= 0; r--) {//Outer Loop for(c = rows-1;c>s;c--) //Inner loop to print first half character System.out.print(" "); System.out.print((char)(r+ascii)); for(c=1;c<(s*2);c++) //Inner loop to print mid gap System.out.print(" "); if(r<rows-1) //Condition to print second half character System.out.print((char)(r+ascii)); s++;//counter //Prints a newline System.out.println(); } } }
Output : Enter Rows : 5 E D D C C B B A A
C Code to Print Inverted V Character Pattern
#include <stdio.h> int main(int argc, char const *argv[]) { printf("Rows : "); //Taking rows as input from user int rows, s = 0, r, c; //starting ASCII value taken 65 int ascii=65; scanf("%d", &rows); for (r = rows - 1; r >= 0; r--) { //Outer Loop for (c = rows - 1; c > s; c--) //Inner loop to print first half character printf(" "); printf("%c",(r+ascii)); for (c = 1; c < (s * 2); c++) //Inner loop to print mid gap printf(" "); if (r < rows - 1) //Condition to print second half character printf("%c",(r+ascii)); s++; //counter //Prints a new line printf("\n"); } return 0; }
Output: Enter Rows : 5 E D D C C B B A A
C++ Code to Print Inverted V Character Pattern
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { int rows, s = 0, r, c; //Starting ASCII value taken 64 int ascii=65; cout << "Rows : "; //Taking rows as input from user cin >> rows; for (r = rows - 1; r >= 0; r--) { //Outer Loop for (c = rows - 1; c > s; c--) //Inner loop to print first half character cout << " "; cout << (char)(r+ascii); for (c = 1; c < (s * 2); c++) //Inner loop to print mid gap cout << " "; if (r < rows - 1) //Condition to print second half character cout << (char)(r+ascii); s++; //counter //Prints a new linee cout << endl; } return 0; }
Output: Enter Rows : 5 E D D C C B B A A
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: