search
HomeBackend DevelopmentC++C++ program to print 8 star patterns

C++ program to print 8 star patterns

It is useful to display star patterns in different formats such as pyramid, square and diamond Commonly used in basic programming and logic construction. How many stars have we seen Number pattern issues when learning loop statements in programming. in the text, We will show the number eight (8) made of stars in C.

In this program, we take line number n, which is the size of the upper half of 8. The lower half will be the same. The eight patterns are as follows

Eight patterns with stars

 * * *
*     *
*     *
*     *
 * * *
*     *
*     *
*     *
 * * *

In the above example, the number of rows, n = 5. For the first five rows, the first half of 8 is is taking shape. When line numbers are 1, n, and n*2, asterisks are printed in Continuous fashion. For the rest of the other lines, only two stars are printed. let us see algorithm for better understanding.

algorithm

  • Enter n as the number of lines in the upper half of 8
  • For i ranging from 1 to 2n - 1, execute
    • If i is 1 or n or i is n*2, then
      • For j ranging from 1 to n, execute
        • If j is 1 or n, then
          • Display blank
        • otherwise
          • Show asterisk (*)
        • If it ends
      • Finish
    • otherwise
      • For k from 1 to n, execute
        • If k is 1 or n, then
          • Show asterisk (*)
        • otherwise
          • Display blank
        • If it ends
      • Finish
    • If it ends
    • Move the cursor to the next line
  • Finish

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 (n = 12)

Eight Pattern for 12 lines.
 ********** 
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
 ********** 
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
 ********** 

in conclusion

The display of numeric modes is one of the more typical problems encountered when using Learn a programming language. This article demonstrates how to use asterisks to display Number 8. (Star). For the number 8, it multiplies the number of rows by 2 to generate n*2 row pattern. Both the upper and lower halves are composed of n lines. Additionally, the width of the pattern is of size n.

The above is the detailed content of C++ program to print 8 star patterns. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
二项式系数表的C程序二项式系数表的C程序Aug 26, 2023 pm 12:49 PM

Givenwithapositiveintegervaluelet&rsquo;ssay&lsquo;val&rsquo;andthetaskistoprintthevalueofbinomialcoefficientB(n,k)where,nandkbeanyvaluebetween0tovalandhencedisplaytheresult.WhatisBinomialCoefficientBinomialcoefficient(n,k)istheorderofcho

最小成本路径的C程序最小成本路径的C程序Aug 26, 2023 pm 06:17 PM

在这里,我们将解决C语言中的最小成本路径问题。这意味着在2D矩阵上完成,其中每个单元格都有一个移动成本。我们必须找到一条从左上角到右下角且行程成本最小的路径。您只能从给定单元格向下和右下遍历单元格。为了解决这个特定问题,动态编程比递归更好。给定成本矩阵cost[][]和位置(m,n),我们必须编写一个函数,返回从(0,0)到达(m,n)的最小路径成本到达(m,n)的路径的总成本是该路径上所有成本的总和(包括源和目的地)。假设−所有成本是积极的。输入矩阵中不存在负成本循环示例查找到(2,2)的最小

在C语言中编写一个程序,打印出N个五角数的序列在C语言中编写一个程序,打印出N个五角数的序列Aug 25, 2023 pm 02:25 PM

程序说明五维体数是帕斯卡三角形的任意一行中第五个数字,从左到右或从右到左开始,起始于5项行14641。这种数字的前几个是1,5,15,35,70,126,210,330,495,715,1001,1365Pentatopenumbersbelongintheclassoffiguratenumbers,whichcanberepresentedasregular,discretegeometricpatterns.Theformulaforthenthpentatopicnumberis$$\l

C++程序以找到给定值的反正切C++程序以找到给定值的反正切Aug 26, 2023 am 10:09 AM

我们在三角学中最常使用的比率包括正弦、余弦、正切等等。您可以使用角度来计算这些比率。如果我们知道比率值,我们还可以使用反三角函数计算角度。本课程将向您展示如何使用C++的反正切(arctan)函数,使用正切值(以弧度为单位)计算角度。atan()函数使用atan()技术和反三角正切函数计算角度。C++标准库包含这个函数。在使用这种方法之前,我们必须导入cmath库。此方法返回以弧度为单位的角度,并采用正切值作为参数。以下使用简单的语法-语法#include<cmath>atan(&l

C++程序:替换特定索引处的字符C++程序:替换特定索引处的字符Aug 25, 2023 pm 10:53 PM

字符串是一组字符。我们也可以将它们称为字符数组。考虑到一个由字符串组成的字符数组,这些字符串具有指定的索引和值。有时候我们可以对字符串进行一些修改,其中一种修改是替换字符通过提供一个特定的索引。在本文中,我们将看到如何替换一个字符从一个specificindexinsideastringusingC++.语法String_variable[&lt;givenindex&gt;]=&lt;newcharacter&gt;在C++中,我们可以使用索引访问字符串字符。在

Java程序创建金字塔和图案Java程序创建金字塔和图案Sep 05, 2023 pm 03:05 PM

如果有人想在Java编程语言方面打下坚实的基础。然后,有必要了解循环的工作原理。此外,解决金字塔模式问题是增强Java基础知识的最佳方法,因为它包括for和while循环的广泛使用。本文旨在提供一些Java程序,借助Java中可用的不同类型的循环来打印金字塔图案。创建金字塔图案的Java程序我们将通过Java程序打印以下金字塔图案-倒星金字塔星金字塔数字金字塔让我们一一讨论。模式1:倒星金字塔方法声明并初始化一个指定行数的整数“n”。接下来,将空间的初始计数定义为0,将星形的初始计数定义为“n+

C程序输入一个由空格分隔的整数序列的数组C程序输入一个由空格分隔的整数序列的数组Aug 25, 2023 am 11:33 AM

问题陈述编写一个C程序,以空格分隔的整数作为数组输入。SampleExamples输入12345输出‘Arrayelementsare-’1,2,3,4,5Explanation的中文翻译为:解释输入包含5个以空格分隔的整数。输入997687542356878967343423输出‘Arrayelementsare-’99,76,87,54,23,56,878,967,34,34,23Explanation的中文翻译为:解释输入包含11个以空格分隔的整数。方法一在这种方法中,我们将把输入中的以空

计算六边形内切圆内的正方形面积的C程序计算六边形内切圆内的正方形面积的C程序Aug 28, 2023 pm 08:41 PM

给定一个正六边形内接的圆内切的正方形,我们需要找到正方形的面积,为此我们需要找到正方形边长和正六边形边长之间的关系。正六边形内接圆的半径的数学公式为,r=A&radic;3/2由于正方形的对角线等于圆的直径,所以半径和边长之间的关系为,a=&radic;r根据正六边形的边长,a=&radic;3A/&radic;2所以,正方形的面积,面积=a2=(&radic;3A/&radic;2)2示例#include<stdio.h>#inclu

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),