Wednesday, 8 April 2020

Pyramid Star Pattern

// C++ Programming Code To Create Pyramid Star Pattern using  Single For Loop



#include<bits/stdc++.h>
using namespace std ;

int main()
{
    int rows, columns ;
    int count=0 ;

    cout<<"Enter height of the stars pyramid : " ;
    cin>>rows ;

    // number of columns in the required pyramid
    columns=(2*rows)-1 ;

    for(int i=1; i<=columns; i++)
    {

        if(i<rows-count)
           cout<<" " ;   //print spaces upto (total_no_of_rows-i) places in each row
        else
            cout<<"*" ;

        if(i==rows+count)
        {
            cout<<"\n" ; // new line after printing each pattern in a row
            count++ ;

            if(i==columns)
                break ;
            i=0 ;        // starts rows from 1st column
        }
    }
}

Pyramid of Star Pattern

// C++ Programming Code To Create Pyramid of Star Pattern





// C program to print full pyramid pattern using stars
#include <stdio.h>
int main()
{
    int i, j, n, k = 0;
    scanf(“%d”,&n);

    for(i = 1; i <= n; ++i, k = 0)

    {
        for(j = 1; j <= n – i; ++j)
        {
            printf(” “);
        }

        while(k != 2 * i-1)

        {
            printf(“* “);
            ++k;
        }

        printf(“\n”);

    }

    return 0;

}