codescracker


c++

C++ Program to Print Star Pyramid Patterns



« Previous Program Next Program »


C++ Pattern Programs

To print patterns in C++ programming, you have to use two for loops, the outer for loop and the inner for loop. The outer for loop is responsible for rows and the inner for loop is responsible for columns.

Pattern Printing Programs in C++

Following are one by one C++ programs to print different-different patterns using stars(*), numbers, and characters or alphabets:

C++ Pattern Program 1

This C++ program prints the half pyramid using stars (*).

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j;
	for(i=0; i<5; i++)
	{
		for(j=0; j<=i; j++)
		{
			cout<<"* ";
		}
		cout<<"\n";
	}
	getch();
}

When the above C++ program is compile and executed, it will produce this output (half pyramid of stars):

C++ program print patterns

C++ Pattern Program 2

This C++ program prints the pattern of stars (*).

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, k=1;
	for(i=0; i<5; i++)
	{
		for(j=0; j<k; j++)
		{
			cout<<"* ";
		}
		k=k+2;
		cout<<"\n";
	}
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

print pattern in C++ Programming

C++ Pattern Program 3

Following C++ program will print the pattern of stars (*).

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, k=8;
	for(i=0; i<5; i++)
	{
		for(j=0; j<k; j++)
		{
			cout<<" ";
		}
		k=k-2;
		for(j=0; j<=i; j++)
		{
			cout<<"* ";
		}
		cout<<"\n";
	}
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program print star pattern

C++ Pattern Program 4

Prints the pattern of stars (*).

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, k=16, tim=1;
	for(i=0; i<5; i++)
	{
		for(j=0; j<k; j++)
		{
			cout<<" ";
		}
		k=k-4;
		for(j=0; j<tim; j++)
		{
			cout<<"* ";
		}
		tim=tim+2;
		cout<<"\n";
	}
	getch();
}

When the above C++ program is compile and executed, it will produce the following output:

print star pattern in C++ Programming

C++ Pattern Program 5

This C++ program will print (half pyramid) pattern of natural numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, n=1;
	for(i=0; i<5; i++)
	{
		for(j=0; j<=i; j++)
		{
			cout<<n<<" ";
			n++;
		}
		cout<<"\n";
	}
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

looping in C++ Programming

C++ Pattern Program 6

This C++ program prints the half pyramid using numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, num=1;
	for(i=0; i<5; i++)
	{
		num=1;
		for(j=0; j<=i; j++)
		{
			cout<<num<<" ";
			num++;
		}
		cout<<"\n";
	}
	getch();
}

When the above C++ program is compile and executed, it will produce the following output (half pyramid using numbers):

C++ program print pattern using looping

C++ Pattern Program 7

Following C++ program ask to the user to enter the number of rows to print the pyramid of stars (*):

#include <iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int i, space, rows, k=0;
    cout<<"Enter the number of rows : ";
    cin>>rows;
    for(i=1; i<=rows; i++)
    {
	for(space=1; space<=(rows-i); space++)
	{
	   cout<<"  ";
	}
	while(k!=(2*i-1))
	{
	   cout<<"* ";
	   k++;
	}
	k=0;
	   cout<<"\n";
    }
    getch();
}

When the above C++ program is compile and executed, it will produce the following output (pyramid of stars):

C++ program print star pyramid

C++ Pattern Program 8

Print half pyramid using continuous characters starting from 'A'.

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j;
	char ch='A';
	for(i=0; i<5; i++)
	{
		for(j=0; j<=i; j++)
		{
			cout<<ch<<" ";
			ch++;
		}
		cout<<"\n";
	}
	getch();
}

When the above C++ program is compile and run, it will produce the following output:

cpp pattern programs

C++ Pattern Program 9

This C++ program prints the triangle of characters or half pyramid using characters.

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j;
	char ch='A';
	for(i=0; i<5; i++)
	{
		for(j=0; j<=i; j++)
		{
			cout<<ch<<" ";
		}
		cout<<"\n";
		ch++;
	}
	getch();
}

When the above C++ program is compile and executed, it will produce the following output (half pyramid using characters):

cpp pattern programs examples

C++ Pattern Program 10

This C++ program prints the inverted half pyramid using stars (*).

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j;
	for(i=0; i<5; i++)
	{
		for(j=5; j>i; j--)
		{
			cout<<"* ";
		}
		cout<<"\n";
	}
	getch();
}

When the above C++ program is compile and executed, it will produce the following output (inverted half pyramid using stars):

cpp star

C++ Pattern Program 11

This C++ program prints the inverted half pyramid using numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, num=1;
	for(i=0; i<5; i++)
	{
		for(j=5; j>i; j--)
		{
			cout<<num<<" ";
			num++;
		}
		cout<<"\n";
		num=1;
	}
	getch();
}

When the above C++ program is compile and run, it will produce the following output (inverted half pyramid using numbers):

pattern programs in cpp

C++ Pattern Program 12

/* C++ Pattern Program of Numbers */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, num=1, incr=1;
	for(i=0; i<5; i++)
	{
		for(j=0; j<incr; j++)
		{
			cout<<num++<<"  ";
			/* in the above statement
			 * first num printed their
			 * previous value, then
			 * incremented by 1 */
		}
		cout<<"\n";
		incr=incr+2;
	}
	getch();
}

The sample run of this C++ program is :

pattern program in c++

C++ Pattern Program No. 13

/* This C++ program prints pattern of numbers */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, k, num=1, decr=8, count=0, temp;
	for(i=0; i<5; i++)
	{
		// this loop is to print spaces
		for(k=0; k<decr; k++)
		{
			cout<<" ";
		}

		// this loop is to give the required value to num
		for(j=0; j<i; j++)
		{
			count++;
		}
		num = count;
		temp = num;

		// this loop is to print the numbers
		for(j=0; j<i; j++)
		{
			cout<<num--<<" ";
			/* from the above statement,
			 * num is first printed, then
			 * decremented by 1 */
		}
		cout<<"\n";
		num = temp;
		decr=decr-2;
	}
	getch();
}

Here is the sample run of the above C++ program:

c++ pattern program

C++ Pattern Program No. 14

/* This C++ program prints the alphabet
 * patterns on the screen */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, incr=1;
	char ch = 'A';
	for(i=0; i<5; i++)
	{
		for(j=0; j<incr; j++)
		{
			cout<<ch<<" ";
			ch++;
		}
		cout<<"\n";
		incr = incr + 2;
	}
	getch();
}

Below is the sample run of this C++ program :

c++ program to print patterns

C++ Pattern Program No. 15

/* Now this C++ program also prints the
 * pattern of alphabet but in different
 * order */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, k, decr=8, count=64, temp;
	char ch='A';
	for(i=0; i<5; i++)
	{
		for(k=0; k<decr; k++)
		{
			cout<<" ";
		}
		for(j=0; j<i+1; j++)
		{
			count++;
		}
		ch = count;
		temp = ch;
		for(j=0; j<i+1; j++)
		{
			cout<<ch--<<" ";
		}
		cout<<"\n";
		ch = temp;
		decr=decr-2;
	}
	getch();
}

Here sample run given of this C++ program:

c++ program print pattern of numbers and stars

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test