Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ Program To Print Character Pattern
Next article icon

C++ Program To Print Character Pattern

Last Updated : 10 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Here we will build a C++ Program To Print Character patterns using 2 Approaches i.e.

  1. Using for loop
  2. Using while loop

Printing 1 character pattern in C++ using different approaches.

1. Using for loop

Input:

rows = 5 

Output: 

A  B B  C C C  D D D D  E E E E E 

Approach 1:

Assign any character to one variable for the printing pattern. The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. Then print the character based on the number of columns.

C++
// C++ program to print character  // pattern using character #include <iostream> using namespace std;  int main() {     int i, j;        // input entering number of rows     int rows = 5;        // taking first character of alphabet     // which is useful to print pattern     char character = 'A';        // first for loop is used to identify number rows     for (i = 0; i < rows; i++) {                // second for loop is used to identify number         // of columns based on the rows         for (j = 0; j <= i; j++) {                        // printing character to get the required             // pattern             cout << character << " ";         }         cout << "\n";                // incrementing character value so that it         // will print the next character         character++;     }     return 0; } 

Output
A  B B  C C C  D D D D  E E E E E 

Approach 2: 

Printing pattern by converting given number into character.

Assign any number to one variable for the printing pattern. The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. After entering into the loop convert the given number into character to print the required pattern based on the number of columns.

C++
// C++ program to print character pattern by // converting number in to character #include <iostream> using namespace std;  int main() {     int i, j;        // input entering number of rows     int rows = 5;        // given a number     int number = 65;        // first for loop is used to identify number rows     for (i = 0; i < rows; i++) {                // second for loop is used to identify number         // of columns based on the rows         for (j = 0; j <= i; j++) {                        // converting number in to character             char character = char(number);                        // printing character to get the required             // pattern             cout << character << " ";         }         cout << "\n";                // incrementing number value so that it         // will print the next character         number++;     }     return 0; } 

Output
A  B B  C C C  D D D D  E E E E E 

2. Using while loops

Input:

rows = 5 

Output: 

A  B B  C C C  D D D D  E E E E E 

Approach 1:

The while loops check the condition until the condition is false. If the condition is true then it enters into the loop and executes the statements.

C++
// C++ program to print character pattern by // converting number in to character #include <iostream> using namespace std;  int main() {      int i = 1, j = 0;        // input entering number of rows     int rows = 5;        // given a character     char character = 'A';        // while loops checks the conditions until the     // condition is false if condition is true then enters     // in to the loop and executes the statements     while (i <= rows) {         while (j <= i - 1) {                        // printing character to get the required             // pattern             cout << character << " ";             j++;         }         cout << "\n";                // incrementing character value so that it         // will print the next character         character++;         j = 0;         i++;     }     return 0; } 

Output
A  B B  C C C  D D D D  E E E E E 

Approach 2:  

Printing pattern by converting given number into character using while loop.

C++
// C++ program to print character pattern by // converting number in to character #include <iostream> using namespace std;  int main() {      int i = 1, j = 0;        // input entering number of rows     int rows = 5;        // given a number     int number = 65;        // while loops checks the conditions until the     // condition is false if condition is true then enters     // in to the loop and executes the statements     while (i <= rows) {         while (j <= i - 1) {                        // converting number in to character             char character = char(number);                        // printing character to get the required             // pattern             cout << character << " ";             j++;         }         cout << "\n";                // incrementing number value so that it         // will print the next character         number++;         j = 0;         i++;     }     return 0; } 

Output
A  B B  C C C  D D D D  E E E E E 

Time complexity: O(n2) where n is given no of rows

Space complexity: O(1)


Next Article
C++ Program To Print Character Pattern

L

laxmigangarajula03
Improve
Article Tags :
  • C++ Programs
  • C++
  • C Pattern Programs
Practice Tags :
  • CPP

Similar Reads

    C++ Program To Print Continuous Character Pattern
    Here we will build a C++ Program To Print Continuous Character patterns using 2 different methods i.e: Using for loopsUsing while loopsInput: rows = 5Output: A B C D E F G H I J K L M N O 1. Using for loopApproach 1: Assign any character to one variable for the printing pattern. The first for loop i
    5 min read
    Program to print reverse character bridge pattern
    For a given value N, denoting the number of Charters starting from the A, print reverse character bridge pattern.Examples : Input : n = 5 Output : ABCDEDCBA ABCD DCBA ABC CBA AB BA A A Input : n = 8 Output : ABCDEFGHGFEDCBA ABCDEFG GFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A Recomm
    5 min read
    C++ Program to Print Cross or X Pattern
    Given a number n, we need to print an X pattern of size n. Input : n = 3Output : $ $ $ $ $Input : n = 5Output : $ $ $ $ $ $ $ $ $Input : n = 4Output : $ $ $$ $$ $ $ We need to print n rows and n columns. So we run two nested loops. The outer loop prints all rows one by one (runs for i = 1 to n). The
    2 min read
    C++ Program to Print the Pattern 'G"
    In this article, we will learn how to print the pattern G using stars and white spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program,
    2 min read
    Program to print the given H Pattern
    Given an integer N, the task is to print the Alphabet H Pattern as given below: 1 N 2 * 3 3 * 2 N * 3 2 1 * 2 3 3 2 * 1 N Examples: Input: N = 3 Output: 1 3 2 2 3 2 1 2 2 1 3 Input: N = 4 Output: 1 4 2 3 3 2 4 3 2 1 3 2 2 3 1 4 Approach: Print the Left value and leave 2 * (index - 1) blank spaces
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences