Program to Print Forward Slash Character Pattern
In the previous article, we have discussed Java Program to print Arrow Character Pattern
In this article we are going to see how to print the forward slash character pattern.
- Java Code to Print Forward Slash Character Pattern
- C Code to Print Forward Slash Character Pattern
- C++ Code to Print Forward Slash Character Pattern
Example-1: When no. of rows : 5 E D C B A
Example-2 When no. of rows : 8 H G F E D C B A
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Approach:
- Enter total row and store it an integer variable say
row
. - Take first for loop to print all the rows.
- Take inner loop to print the column values.
- Print the character symbol if
c==(row+1-r)
.
Java Code to Print Forward Slash Character Pattern
import java.util.Scanner; class Main { public static void main(String[] args) { int r,c,row; // starting ASCII value taken 65 int ascii=65; System.out.print("Enter no of rows : "); // create object of Scanner class Scanner sc= new Scanner(System.in); // Store the input from user to row row=sc.nextInt(); // loop for no of rows for(r=1;r<=row;r++) { // loop for printing character symbol for(c=1;c<=row;c++) { // printing characters and spaces based on condition if(c <= (row+1-r)) { if( c == (row+1-r) ) System.out.print((char)(c+ascii)+" "); else System.out.print(" "); } } System.out.println(""); } } }
Output: Enter no of rows : 8 H G F E D C B A
C Code to Print Forward Slash Character Pattern
#include<stdio.h> int main() { int r,c,row; int ascii=64; printf("Enter no of rows : "); scanf("%d",&row); for(r=1;r<=row;r++) { for(c=1;c<=row;c++) { if(c <= (row+1-r)) { if( c == (row+1-r) ) printf("%c",(c+ascii)); else printf(" "); } } printf("\n"); } return 0; }
Output: Enter no of rows : 8 H G F E D C B A
C++ Code to Print Forward Slash Character Pattern
#include <iostream> using namespace std; int main() { int r,c,row; // starting ASCII value taken 64 int ascii=64; cout<<("Enter no of rows : "); cin>>row; for(r=1;r<=row;r++) { for(c=1;c<=row;c++) { if(c <= (row+1-r)) { if( c == (row+1-r) ) cout<< (char)(c+ascii) << " "; else cout<<(" "); } } cout<<("\n"); } return 0; }
Output: Enter no of rows : 8 H G F E D C B 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: