Hello Readers,

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


Logic to print the pattern of numbers(Full-Pyramid)


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 i.e. "users input" 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 (j<=n),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 print the value of "k"

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 1 i.e. (l>=1)

9. Inside this loop print the value of 'I'.

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. 

7.In this manner the Full pyramid of the numbers will be formed.



Process/Execution of loops:

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

Now, In the Loop

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.e j<=3

it  will print " "

next loop,

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

it will print  1

next loop,

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

it will print  2

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=2, satisfies the condition k<=i i.e k<=3

it will print  2

next loop,

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

it will print  1

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.e j<=3

it  will print " "

next loop,

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

it will print  1

next loop,

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

it will print  2

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

it will print  3

next loop,

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

it will print  3

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

the Output is:-

1

      121

    12321



Document

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++)
        {
            printf("%d",c);
        }
        for(d=a-1;d>=1;d--)
        {
            printf("%d", d);
        }
        printf("\n");
        
    
    }

 }

    

Output for C


C++


//**HELPFORCODERS**
//This is a Pattern project which prints
//          1
//	   121
//	  12321
//	 1234321
//	123454321

 #include <iostream>
 using namespace std;  

 int main()  
 {  
    int i,j,k,l,n;    
    cout<<"Enter the Range\n";    
    cin>>n;    
    for(i=1;i<=n;i++)    
    {    
        for(j=1;j <=n-i;j++)    
        {    
            cout<<" ";    
        }    
        for(k=1;k <=i;k++)    
        {    
            cout<<k;    
        }    
        for(l=i-1;l>=1;l--)    
        {    
            cout<<l;    
        }    
        cout<<"\n";    
    }    
 return 0;  
 }
 
    

Output for C++


Java


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

public class main {

  public static void main(String[] args) 
      { 
        System.out.println("Hello, User!");
        System.out.println("Enter the value of n");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        
        for(int i=1;i<=n;i++)
        {
          for(int j=1;j<=n-i;j++)
          {
            System.out.print("  ");
          }
          for(int k=1;k<=i;k++)
          {
            System.out.print(k);
          }
          for(int l=i-1;l>=1;l--)
          {
            System.out.print(l);
          }
          System.out.print("\n");
       	}
      }
  }

Output for JAVA


Python


		
n = int(input("enter value of n\n>>> "))
for i in range (1, n +1 ):
	for j in range (1, n - i + 1):
		print(end = " ")
	for j in range (1, i + 1):
		print(j, end = "")
	for j in range (i - 1, 0,-1):
		print(j, 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