
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Print 8 Star Pattern
Displaying star patterns in different formats like pyramids, squares, and diamonds is very common in fundamental programming and logic building. We have seen several stars and number pattern problems while learning looping statements in programming. In this article, we will display the number Eight (8) made with stars in C++.
In this program, we take the line number n which is the size of the upper half of the eight. The lower half will be identical. The eight-pattern will look like below
Eight-Pattern with stars
* * * * * * * * * * * * * * * * * * * * *
In the above example, the number of lines, n = 5. For the first five lines, the upper half of 8 is being formed. When the line numbers are 1, n, and n*2, the stars are printed in a consecutive fashion. For the rest of the other lines, only two stars are printed. Let us see the algorithm for a better understanding.
Algorithm
- Take input n as the number of lines for the upper half of 8
- for i ranging from 1 to 2n - 1, do
- if i is 1 or n or i is n*2, then
- for j ranging from 1 to n, do
- if j is 1 or n, then
- display blank space
- otherwise
- display asterisk (*)
- end if
- if j is 1 or n, then
- end for
- for j ranging from 1 to n, do
- otherwise
- for k ranging from 1 to n, do
- if k is 1 or n, then
- display asterisk (*)
- otherwise
- display blank space
- end if
- if k is 1 or n, then
- end for
- for k ranging from 1 to n, do
- end if
- move cursor to the next line
- if i is 1 or n or i is n*2, then
- end for
Example
#include <iostream> using namespace std; void solve( int n ){ for ( int i = 1; i <= n * 2 - 1; i++ ) { if ( i == 1 || i == n || i == n * 2 - 1 ) { for ( int j = 1; j <= n; j++ ) { if ( j == 1 || j == n ) { cout << " "; } else { cout << "*"; } } } else { for ( int k = 1; k <= n; k++ ) { if ( k == 1 || k == n ) { cout << "*"; } else { cout << " "; } } } cout << "\n"; } } int main(){ int n = 7; cout << "Eight Pattern for " << n << " lines." << endl; solve( n ); }
Output
Eight Pattern for 7 lines. ***** * * * * * * * * * * ***** * * * * * * * * * * *****
Output (With n = 12)
Eight Pattern for 12 lines. ********** * * * * * * * * * * * * * * * * * * * * ********** * * * * * * * * * * * * * * * * * * * * **********
Conclusion
The display of numerical patterns is one of the more typical issues encountered when learning programming languages. This article demonstrated how to use asterisks to display the number 8. (stars). For the number 8, it multiplies the number of lines by two to produce n*2 lines of the pattern. Both the upper and lower halves are made up of n lines. Additionally, the pattern's breadth is of size n.
- Related Articles
- Java Program to Print 8 Star Pattern
- Golang Program To Print 8 Star Pattern
- Swift program to print 8 star pattern
- Haskell Program to Print 8-Star Pattern
- C++ Program to Print X Star Pattern
- C++ Program to Print Square Star Pattern
- C program to print hollow rectangle star pattern
- C++ Program to Print Right Triangle Star Pattern
- C++ Program to Print Left Triangle Star Pattern
- C++ Program to Print Upper Star Triangle Pattern
- C++ Program to Print Upward Triangle Star Pattern
- C++ Program to Print Downward Star Triangle Pattern
- Program to print Hut Star pattern
- C++ Program to Print Hollow Right Triangle Star Pattern
- C++ Program to Print Mirror Upper Star Triangle Pattern
