Java Program to Print Backslash Character Pattern

Program to Print Backslash Character Pattern

In the previous article, we have discussed Java Program to print Forward Slash Character Pattern

In this article we are going to see how to print the Backslash character pattern.

Example-1:

Enter the no of rows = 5
A               
  B             
    C           
      D         
        E
Example-2:

Enter the no of rows = 8

A               
  B             
    C           
      D         
        E       
          F     
            G   
              H

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Approach:

  • Enter total row and store it an integer variable say row.
  • Take first for loop to print all the rows.
  • Take a inner loop to print the column values.
  • Then go on printing the characters according to the iteration.

Java Code to Print Backslash Character Pattern

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
     
    // starting ASCII value taken 64
    int ascii=64;  
    int r,c,row;
        
    Scanner sc= new Scanner(System.in);
    // Take user input for no fo rows 
    System.out.print("Enter the no of rows = ");
    // store the input value in row
    row=sc.nextInt();
    
    // loop for no of rows
   for(r=1; r<=row; r++)
   {   
       // inner loop to print symbol
      for(c=1; c<=row; c++)
      {   
          // if row and column have same value print symbol     
         if(r==c)      
            System.out.print((char)(c+ascii)+" ");      
         else          
            System.out.print("  ");      
      } 
      System.out.println("");
   } 
    }
}
Output:

Enter the no of rows = 8

A               
  B             
    C           
      D         
        E       
          F     
            G   
              H

C Code to Print Backslash Character Pattern

#include<stdio.h>
int main()
{
   int r,c,k,row;
   int ascii=64;
   printf("Enter the no of rows = ");
   scanf("%d",&row);
   for(r=1; r<=row; r++)
   {    
      for(c=1; c<=row; c++)
      {        
         if(r==c)      
            printf("%c ",(c+ascii));      
         else          
            printf("  ");      
      }        
      printf("\n");
   }    
   return 0;
}
Output:

Enter the no of rows = 8

A               
  B             
    C           
      D         
        E       
          F     
            G   
              H

C++ Code to Print Backslash Character Pattern

#include <iostream>
using namespace std;
int main()
{
   int r,c,row;
   // starting ASCII value taken 64
   int ascii=64;
   cout<<"Enter the no of rows = ";
   cin>>row;
   for(r=1; r<=row; r++)
   {    
      for(c=1; c<=row; c++)
      {        
         if(r==c)      
            cout<< (char)(c+ascii) << " ";      
         else          
            cout<<("  ");      
      }        
      cout<<("\n");
   }    
   return 0;
}
Output:

Enter the no of rows = 8

A               
  B             
    C           
      D         
        E       
          F     
            G   
              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: