Hello Readers,

In this post we are going to see, how to print FULL PYRAMID OF SPECIAL CHARACTERS in C++, JAVA, PYTHON which is as follows:-


Logic to print the pattern of Special Characters(Full-Pyramid Pattern)


1. Assign five variables as row, i, j, k, l.

2. Now use the variable row to take the input from the user i.e. the number of rows

3. Now using "for loop" start the iterator(i) from 1 till the no of rows user want -1  i.e.(n-1) use post incrementation for 'i' value (This Loop is for row management)

4.Using nested for loop(loop inside a loop) take another iterator as 'j' and initialize it as 1, its value will last till the input value-i+1 (j<=n-i+1),use post incrementation for 'j'. (This loop if for Column Management).

5. Now inside this loop we will print Nothing i.e.(" ") for space between two columns.

6. Outside of this loop make another loop and use 'k' as its iterator, start the value of 'k' with 1, till the value of i, use post incrementation for 'k'.

7. Now inside this loop use if condition i.e.(if i%2==0) if the condition is satisfied then print "*" or else print "#".

8. Outside this loop take another loop with iterator 'l', initialize it as i-1, its value will get post incremented till the value of 'l' becomes greater than or equals to 0 i.e. (l>=0)

9. Now inside this loop use if condition i.e.(if i%2==0) if the condition is satisfied then print "*" or else print "#".

10. Outside this loop print the statement for the next line i.e.("\n")

11. As the loop gets executed by the compiler the number will get printed one by one according to the code. 

12. In this manner the Full pyramid pattern will be formed.



Process/Execution of loops:

eg. let's say user has given the input as "3",

Now, In the Loop,

i=0, and it also satisfies the condition i.e. i<=3 hence it will get incremented by 1  i.e. i++

next loop,

j=0, satisfies the condition j<=n-i+1 i.e j<=3

it  will print " "

next loop,

k=0, satisfies the condition k<=i i.e k<=3

Now it will check the if condition i.e.(i%2==0)

In this case it satisfies the condition and hence it will print "*"

next loop,

l=(i-1)i.e. 2, satisfies the condition l>=1 

Here also it will check the if condition i.e.(i%2==0)

In this case it satisfies the condition and hence it will print "*"

then, it will go to next line "\n"

now,

i=1, and it also satisfies the condition i.e. i<=3 hence it will get incremented by 1  i.e. i++

next loop,

j=1, satisfies the condition j<=n-i+1 i.e j<=3

it  will print " "

next loop,

k=1, satisfies the condition k<=i i.e k<=3

Now it will check the if condition i.e.(i%2==0)

In this case it doesn't satisfies the condition and hence it will print "#"

next loop,

l=(i-1)i.e. 1, satisfies the condition l>=2 

here also it will check the if condition i.e.(i%2==0)

In this case it doesn't satisfies the condition and hence it will print "#"

then, it will go to next line "\n"

now,

i=2, and it also satisfies the condition i.e. i<=3 hence it will get incremented by 1  i.e. i++

next loop,

j=2, satisfies the condition j<=n i.e j<=3

it  will print " "

next loop,

k=1, satisfies the condition k<=i  i.e. k<=3

Now it will check the if condition i.e.(i%2==0)

In this case it doesn't satisfies the condition and hence it will print "*"

next loop,

l=(i-1)i.e. 2, satisfies the condition l>=3 

Here also it will check the if condition i.e.(i%2==0)

In this case it doesn't satisfies the condition and hence it will print "*"

then, it will go to next line "\n"

now,

i=3, and it also satisfies the condition i.e. i<=3 hence it will get incremented by 1  i.e. i++

next loop,

j=3, satisfies the condition j<=n  i.e. j<=3

it  will print " "

next loop,

k=3, satisfies the condition k<=i  i.e. k<=3

Now it will check the if condition i.e.(i%2==0)

In this case it doesn't satisfies the condition and hence it will print "#"

next loop,

I=(i-1)i.e. 3, satisfies the condition I>=1

Here also it will check the if condition i.e.(i%2==0)

In this case it doesn't satisfies the condition and hence it will print "#"

then, it will go to next line "\n"

the Output is:-

*

       ###

      *****

   #######




C


#include<stdio.h>
int main()
{
    int a,b,c,d,n;
    printf("enter the number of rows");
    scanf("%d", &n);
    for(a=1;a<=n;a++)
    {
        for(b =1; b <=n-a;b++)
        {
            printf(" ");
        }
        for(c=1;c<=a;c++)
        {
            if(a%2==0)
            {
                printf("#");
            }
            else
            {
                printf("*");
            }
            
        }
        for(d=a-1;d>=1;d--)
        {
            if(a%2==0)
            {
                printf("#");
            }
            else
            {
                printf("*");
            }
        }
        printf("\n");
        
    
    }

 }
    


Output For C


C++


//**HELPFORCODERS**
//This is a Pattern project which prints
//    *
//   ###
//  *****
// #######
 #include <iostream>
 using namespace std;

 int main()
 {
    int n;
    cout << "Enter the number of row\n";
    cin >> n;

    for (int i = 0; i <= n-1; i++)
    {
        for (int j = 1; j <= n - i + 1; j++)
        {
            cout << " ";
        }

        for (int k = 0; k <= i; k++)
        {
            if (i % 2 == 0)
            {
                cout << "*";
            }
            else
            {
                cout << "#";
            }
        }
        for (int l = i - 1; l >= 0; l--)
        {
            if (i % 2 == 0)
            {
                cout << "*";
            }
            else
            {
                cout << "#";
            }
        }
        cout << endl;
    }
    return 0;
 }
 
    


Output For C++


Java


package com.company;
import java.util.*;

public class Main
{

    public static void main(String[] args) {

        System.out.println("Enter the number of rows");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();

        for (int i = 0; i <= n-1; i++)
        {
            for (int j = 1; j <= n - i + 1; j++)
            {
                System.out.print(" ");
            }

            for (int k = 0; k <= i; k++)
            {
                if (i % 2 == 0)
                {
                    System.out.print("*");
                }
                else
                {
                    System.out.print("#");
                }
            }
            for (int l = i - 1; l >= 0; l--)
            {
                if (i % 2 == 0)
                {
                    System.out.print("*");
                }
                else
                {
                    System.out.print("#");
                }
            }
            System.out.print("\n");
        }

    }
}

Output for JAVA


Python



n= int(input(" Enter the number of rows"))
for i in range (1, n +1 ):
	for j in range (1, n - i + 1):
		print(end = " ")
	for j in range (1, i + 1):
		if i %2 == 0:
			print("#", end = "")
		else:
			print("*", end = "")
	for j in range (i - 1, 0,-1):
		if i %2 == 0:
			print("#", end = "")
		else:
			print("*", end = "")
	print("\n")

Output for PYTHON

//ENJOY CODING\\



Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post