Java Program to Print Right Angled-Triangle with odd number of Characters Pattern

Printing Right Angled-Triangle with odd number of Characters Pattern

In this program we are going to see how to print the right angled triangle with Odd number increasing character pattern.

 

Example-1

When character=h & row value=3

h
h i j
h I j k l
Example-2:

When character=A & row value=5

A
A B C
A B C D E
A B C D E F G
A B C D E F G H I

Now, let’s see the actual program to print it.

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 the character and store it in a variable c.
  • Then enter total row and store it in an integer variable row_count
  • Take one outer for loop to iterate the rows.
  • Take one inner loop to iterate the columns and print the character.
  • After each iteration print a new line.

JAVA CODE:

import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        //Taking character to be printed as input from the user
        System.out.print("Character : ");
        char c = scan.next().charAt(0);
    
        //Taking number of rows as input from the user
        System.out.print("Rows : ");
        int row_count = scan.nextInt();
    
        int row, col;
        //Converting the character to its ascii value
        int asciiValue = (int) c;
    
        //Checks whether the character is a letter or not
        if(asciiValue>=65&&asciiValue<=122)
        {
            
            //Outer loop to iterate rows
            for (row = 1; row <= row_count; row++)
            {
                //Reseting the character after each iteration
                asciiValue = c;
                //Inner loop to iterate columns
                for (col = 1; col <= row * 2 - 1; col++)
                {
                    System.out.print((char)(asciiValue)+" ");
                    asciiValue++;
                }
                //Prints a new line
                System.out.println();
            }
        }
    }
}
Output:

Character : A
Rows : 5

A
A B C
A B C D E
A B C D E F G
A B C D E F G H I

C CODE:

#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 : ");
    int row_count;
    scanf("%d", &row_count);

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

    //Checks whether the character is a letter or not
    if (asciiValue >= 65 && asciiValue <= 122)
    {
        //Outer loop to iterate rows
        for (row = 1; row <= row_count; row++)
        {
            //Reseting the character after each iteration
            asciiValue = c;
            //Inner loop to iterate columns
            for (col = 1; col <= row * 2 - 1; col++)
            {
                printf("%c ", (char)(asciiValue));
                asciiValue++;
            }
            //Prints a newline
            printf("\n");
        }
    }
    return 0;
}
Output:

Character : A
Rows : 5

A
A B C
A B C D E
A B C D E F G
A B C D E F G H I

C++ CODE:

#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 : ";
    int row_count;
    cin >> row_count;

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

    //Checks whether the character is a letter or not
    if (asciiValue >= 65 && asciiValue <= 122)
    {
        //Outer loop to iterate rows
        for (row = 1; row <= row_count; row++)
        {
            //Reseting the character after each iteration
            asciiValue = c;
            //Inner loop to iterate columns
            for (col = 1; col <= row * 2 - 1; col++)
            {
                cout << (char)asciiValue << " ";
                asciiValue++;
            }
            //Prints a newline
            cout << endl;
        }
    }
    return 0;
}

Output:

Character : A
Rows : 5

A
A B C
A B C D E
A B C D E F G
A B C D E F G H I

Related Java Star Pattern Programs: