Printing letters of a string in Inverted Right Angled Triangle Pattern
In the previous article, we have discussed Java Program to Print Pyramid with Column wise Same Character Pattern
In this program we are going to see how to print Letters of a string in inverted right angle character pattern.
- Java Code to Print letters of a string in Inverted Right Angled Triangle Character Pattern
- C Code to Print letters of a string in Inverted Right Angled Triangle Character Pattern
- C++ Code to Print letters of a string in Inverted Right Angled Triangle Character Pattern
Example-1 When String value= Example Example Exampl Examp Exam Exa Ex E
Example-2: When String value= PatternString PatternString PatternStrin PatternStri PatternStr PatternSt PatternS Pattern Patter Patte Patt Pat Pa P
Now, let’s see the actual program to print it.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Approach:
- Enter the string and store it in an variable
str
. - 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 newline.
Java Code to Print letters of a string in Inverted Right Angled Triangle Character Pattern
import java.util.Scanner; 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 = len-1; row>=0; 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 BtechGeek BtechGee BtechGe BtechG Btech Btec Bte Bt B
C Code to Print letters of a string in Inverted 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 for (row = len - 1; row >= 0; 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 BtechGeek BtechGee BtechGe BtechG Btech Btec Bte Bt B
C++ Code to Print letters of a string in Inverted 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 = len - 1; row >= 0; 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 BtechGeek BtechGee BtechGe BtechG Btech Btec Bte Bt B
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: