Hello Readers,

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

                                    1

                                    2 2

                                    3 3 3

                                    4 4 4 4

Logic to print the pattern of numbers(Half-Pyramid):-

1.Assign three variables as row, i, j.

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 value of 'i' (j<=i),use post incrementation for 'j'. (This loop if for Column Management).

5.Now inside this loop we will print the value of first iterator(i) with some space.

6.Outside of this loop give a command to go to next line i.e. "\n"

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

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



Process/Execution of loops:-

** 'i' is the number of row and 'j' is the number of column **

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

it will print  1

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

now,

i=2, satisfies condition i<=3

as 'j' was post incremented now,

j=2, satisfies condition j<=i i.e. j<=2

it will print  2 2  

the it will go to next line "\n"

now,

i=3, satisfies condition i<=3

as 'j' was get post incremented now,

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

it will print  3 3 3

then it will go to next line "\n"

now,

i=4, but it does not satisfies the condition i<=3, therefore the execution of program will be stopped.

the Output is:-

1

2 2

3 3 3

Note: The Above Example Program does not follow any Syntax its just for Explanation Purpose.


  



C


 /*
  
  level 1.1
 1
 22
 333
 4444
 55555


  
  */
 #include<stdio.h>
 int main()
 {

    // declaring variables
    int i,j,n;
    // Asking the user to input the number of rows
    printf("Enter the number of rows \n");

    // Taking the value of the user in the variable 'n'
    scanf("%d \n",&n);

    // First for loop for rows management
    for(i=1;i<=n;i++)
    {

        // Second for loop for columns management
        for(j=1;j<=i;j++)
        {

            // printing the pattern
            printf("%d",i);
        }

        // printing the code for next line 
        printf("\n");
    }
    return 0;
 }
    

Output for C


C++


          
 //**HELPFORCODERS**
 //This is a Pattern project which prints
 //1
 //22
 //333
 //4444
 //55555

 #include<iostream>
 using namespace std;

 //This is the main Function
 int main()
 {
    cout<<"This is the Program to print the Pattern of HALF PYRAMID OF NUMBERS\n";
    int row,i,j;
    
    //Printing a statement to take user input
    cout<<"Enter the number of rows"<<endl;
    
    //Taking user input for number of rows
    cin>>row;
    
    //First for loop
    for(i=1;i<=row; i++)
    {
        //Second for loop
        for(j=1;j<=i;j++)
        {
            //Printing the pattern with spaces
            cout<<i<<" ";
        }
        //This command helps to move to next line after printing the number
        cout<<endl;
    }
    
    return 0;
 }
          
    

Output for C++


Java


 package com.company;
 import java.util.*;
 
 /*
  
  level 1.1
 1
 22
 333
 4444
 55555


  
  */
 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();
    
    System.out.println("\nhere is the 1st pattern\n");
    for (int i = 1; i <= n; i++){
      for (int j = 1; j <= i; j++){
        System.out.print(i);
      }
      System.out.println("");
     }
   }
 } 

Output for JAVA


Python


		
print("******* Hello User *******")
#take the input from the user
n=int(input("enter number \n"))

#First Loop for no. of Rows
for i in range(1,n+1): 	
  
  #Second loop for no. of Columns
	for j in range(1,i+1):
	  
	  #printing the value
		print(i,end="")
		
		
	# (\r)=End of the row
	print("\r") 			

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