Java Program to Print letters of a string in Right Angled Triangle Character Pattern

Printing letters of a string in Right Angled Triangle Character Pattern

In the previous article, we have discussed Java Program to Print letters of a string in Inverted Right Angled Triangle Character Pattern

In this program we are going to see how to print Letters Of a string in right angle character pattern.

Example-1

When String value= Example

E
Ex
Exa
Exam
Examp
Exampl
Example
Example-2:

When String value= PatternString

P
Pa
Pat
Patt
Patte
Patter
Pattern
PatternS
PatternSt
PatternStr
PatternStri
PatternStrin
PatternString

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

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 the string and store it in an variable string.
  • Take one outer for loop to iterate the rows,
  • Inside the for loop, take one for loop, one to print the string.
  • After each iteration print a new line.

Java Code to Print letters of a string in Right Angled Triangle Character Pattern

import java.util.Scanner;
public class Main
{
public static void main(String[] args)  
{  
    Scanner scan = new Scanner(System.in);

    //Taking string as input from the user
    System.out.print("String : ");
    String str = scan.nextLine();
    scan.close();

    int row,col;
    
    //Length of the string
    int len = str.length();
    //Coverting the String to a character array
    char[] charArray = str.toCharArray();

    //Outer loop to iterate rows
    for(row = 0; row<=len-1; row++)
    {
        //Inner loop to iterate columns and print string
        for(col = 0; col<=row; col++)
        {
            System.out.print(charArray[col]);
        }
        //Prints a newline
        System.out.println();
    }
}  
}
Output:

String : BtechGeeks
B
Bt
Bte
Btec
Btech
BtechG
BtechGe
BtechGee
BtechGeek
BtechGeeks

C Code to Print letters of a string in Right Angled Triangle Character Pattern

#include <stdio.h>
#include <string.h>

int main()
{
    //Taking string as input from the user
    printf("String : ");
    char str[100];
    gets(str);

    int row, col;

    int len = strlen(str); //Length of the string                                     //Outer loop to iterate rows

    //Outer loop to iterate rows
    for (row = 0; row <= len - 1; row++)
    { //Inner loop to iterate columns and print string
        for (col = 0; col <= row; col++)
        {
            printf("%c", str[col]);
        }
        //Prints a newline
        printf("\n");
    }
    return 0;
}

Output:

String : BtechGeeks
B
Bt
Bte
Btec
Btech
BtechG
BtechGe
BtechGee
BtechGeek
BtechGeeks

C++ Code to Print letters of a string in Right Angled Triangle Character Pattern

#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
int main(int argc, char const *argv[])
{
    //Taking string as input from the user
    cout << "String : ";
    string str;
    cin >> str;

    char charArray[100];
    //Copying the String to a character array
    strcpy(charArray, str.c_str());

    int row, col;

    //Length of the character Array
    int len = strlen(charArray);

    //Outer loop to iterate rows
    for (row = 0; row <= len - 1; row++)
    {
        //Inner loop to iterate columns and print string
        for (col = 0; col <= row; col++)
        {
            cout << charArray[col];
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}
Output:

String : BtechGeeks
B
Bt
Bte
Btec
Btech
BtechG
BtechGe
BtechGee
BtechGeek
BtechGeeks

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: