Studytonight is now part of the GUVI universe. Explore GUVI →
🤩 New Cool Developer Tools for you. Explore →
FREE JavaScript Video Series Start Learning →
Signup/Sign In
Tests
MCQs to test your knowledge.
Compilers
Compilers to execute code in browser.
Index
LAST UPDATED: OCTOBER 31, 2020

C++ Reverse Half Pyramid Pattern using Characters

Hello Everyone!

In this tutorial, we will learn how to print a Reverse Half Pyramid Structure using Characters, in the C++ programming language.

All such patterns using * or alphabets or numbers are achieved by making use of the nested loop structures by knowing how to iterate and till where to iterate.

We believe that all the patterns covered in this section will help you understand this concept and visualize it better while forming your own patterns, as such questions are very frequently asked in various interviews with a slight modification.

Code:

#include <iostream>
using namespace std;
int main()
{
 cout << "\n\nWelcome to Studytonight :-)\n\n\n";
 cout << " ===== Program to print a Reverse Half Pyramid using Characters ===== \n\n";
 //i to iterate the outer loop and j for the inner loop
 int i, j, rows, cols;
 //to denote the range of characters in each row
 char c, first,last; 
 cout << "Enter the number of rows in the pyramid: ";
 cin >> rows;
 cout << "\n\nThe required Reverse Pyramid pattern containing " << rows << " rows is:\n\n";
 //outer loop is used to move to a particular row
 for (i = 1; i <= rows; i++)
 {
 first = 'A';
 last = first + rows -i ;
 c = 'A';
 cols = rows-i+1;
 //to display that the outer loop maintains the row number
 cout << "Row # " << i << " contains characters from " << first << " to " << last << " : ";
 
 //inner loop is used to decide the number of * in a particular row
 for (j = 1; j<= cols; j++)
 {
 cout << c << " ";
 c+=1;
 }
 cout << endl;
 }
 cout << "\n\n";
 return 0;
}

Output:

C++ reverse half pyramid using characters

We will highly recommend you to first draw such patterns line by line on a paper before getting into programming them, as it will help you understand the nested structure better.

Keep Learning : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.

Learn to Code
Learn and practice coding side-by-side.
NEW
C language Course
115+ coding exercises
Javascript Course
85+ coding exercises

AltStyle によって変換されたページ (->オリジナル) /