Java Program to Print Right Pascal Character Pattern

Program to Print Right Pascal Character Pattern

In the previous article, we have discussed Java Program to Print Sand Glass Character Pattern

In this program we are going to see how to print the right pascal character pattern.

Example-1

When row value=7

A
AB
ABC
ABCD
ABC
AB
A
Example-2:

When row value=11

A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDE
ABCD
ABC
AB
A

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 total row and store it in an integer variable row_count.
  • Take two outer for loops, one for the upper half and the other for the bottom.
  • Inside both the loops, take two inner for loops to print the space and the characters.
  • After each iteration print a new line.

Java Code to Print Right Pascal Character Pattern

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        //Taking character to be printed as input from the user
        System.out.print("Character : ");
        Scanner scan = new Scanner(System.in);
        char c = scan.next().charAt(0);
    
        //Taking number of rows as input from the user
        System.out.print("Rows(enter odd number) : ");
        int row_count = scan.nextInt();
        row_count = (row_count) / 2;
        //Making the row count half to print each half of the pattern
    
        int row, col;
    
        int asciiValue = (int)c;
        //Converting the character to its ascii value
    
        for (row = 0; row <= row_count; row++)
        {
            //Outer loop to print upper half
            for (col = 0; col <= row; col++)
            {
                System.out.print((char)(asciiValue + col));
            }
            System.out.println();
            //Prints a newline
        }
    
        for (row = row_count - 1; row >= 0; row--)
        {
            //Inner loop to print upper half
            for (col = 0; col <= row; col++)
            {
                System.out.print((char) (asciiValue + col));
            }
            System.out.println();
            //Prints a newline
        }
    }  
}

Output:

Character : A
Rows(enter odd number) : 5

A
AB
ABC
AB
A

C Code to Print Right Pascal Character Pattern

#include <stdio.h>

int main()
{
    //Taking character to be printed as input from the user
    printf("Character : ");
    char c;
    scanf("%c", &c);

    //Taking number of rows as input from the user
    printf("Rows(enter odd number) : ");
    int row_count;
    scanf("%d", &row_count);
    row_count = (row_count) / 2;
    //Making the row count half to print each half of the pattern

    int row, col;

    int asciiValue = (int)c;
    //Converting the character to its ascii value

    for (row = 0; row <= row_count; row++)
    {
        //Outer loop to print upper half
        for (col = 0; col <= row; col++)
        {
            printf("%c ", (asciiValue + col));
        }
        printf("\n");
        //Prints a newline
    }

    for (row = row_count - 1; row >= 0; row--)
    {
        //Inner loop to print upper half
        for (col = 0; col <= row; col++)
        {
            printf("%c ", (asciiValue + col));
        }
        printf("\n");
        //Prints a newline
    }

    return 0;
}

Output:

Character : A
Rows(enter odd number) : 5

A
AB
ABC
AB
A

C++ Code to Print Right Pascal Character Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //Taking character to be printed as input from the user
    cout << "Character : ";
    char c;
    cin >> c;

    //Taking number of rows as input from the user
    cout << "Rows(enter odd number) : ";
    int row_count;
    cin >> row_count;
    row_count = (row_count) / 2;
    //Making the row count half to print each half of the pattern

    int row, col;
    //Converting the character to its ascii value
    int asciiValue = (int)c;

    for (row = 0; row <= row_count; row++)
    {
        //Outer loop to print upper half
        for (col = 0; col <= row; col++)
        {
            cout << (char)(asciiValue + col);
        }
        cout << endl;
        //Prints a newline
    }

    for (row = row_count - 1; row >= 0; row--)
    {
        //Inner loop to print upper half
        for (col = 0; col <= row; col++)
        {
            cout << (char)(asciiValue + col);
        }
        cout << endl;
        //Prints a newline
    }
    return 0;
}
Output:

Character : A
Rows(enter odd number) : 5

A
AB
ABC
AB
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: