Program to Print V Character Pattern
In the previous article, we have discussed Java Program to Print Inverted V Symbol Character Pattern
In this article, we are going to see how to print the character pattern of the letter V.
- Java Code to Print V Character Pattern
- C Code to Print V Character Pattern
- C++ Code to Print V Character Pattern
Example-1 When the number of rows =5 A I B H C G D F E
Example-2 When the number of rows =8 A O B N C M D L E K F J G I H
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Approach
- Enter total row and store it in an integer variable say
row
. - Take a inner for loop to print the column values.
- Keep iterating and print the values.
Java Code to Print V Character Pattern
import java.util.Scanner; public class Main { public static void main(String[] args) { int row; // starting ASCII value taken 64 int ascii=64; // create scanner class to take user input Scanner sc= new Scanner(System.in); System.out.print("Enter no of row = "); row=sc.nextInt(); int c, r; int x = 1; // store row*2-1 value in y int y = row * 2 - 1; // loop to iterate through rows for (r = 1; r <= row; r++) { // iterate inner loop from 1 till row*2 for (c = 1; c <= row * 2; c++) { // if c= x or y print the symbol, else space if (c == x || c == y) { System.out.print((char)(c+ascii)); } else { System.out.print(" "); } } // increment x x++; // decrement y y--; System.out.println(""); } } }
Output: Enter no of row = 8 A O B N C M D L E K F J G I H
C Code to Print V Character Pattern
#include <stdio.h> int main() { printf("Enter no of row = "); int row; int ascii=64; scanf("%d", &row); int c, r; int x = 1; int y = row * 2 - 1; for (r = 1; r <= row; r++) { for (c = 1; c <= row * 2; c++) { if (c == x || c == y) { printf("%c",(c+ascii)); } else { printf(" "); } } x++; y--; printf("\n"); } }
Output: Enter no of row = 8 A O B N C M D L E K F J G I H
C++ Code to Print V Character Pattern
#include <iostream> using namespace std; int main() { cout<<"Enter no of row = "; int row; cin>>row; int c, r; int ascii=64; int x = 1; int y = row * 2 - 1; for (r = 1; r <= row; r++) { for (c = 1; c <= row * 2; c++) { if (c == x || c == y) { cout<<(char)(c+ascii); } else { cout<<" "; } } x++; y--; cout<<"\n"; } }
Output: Enter no of row = 8 A O B N C M D L E K F J G I H
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: