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:-
                                      5 4 3 2 1
                                      4 3 2 1
                                     3 2 1
                                     2 1
                                     1
Logic to print the pattern of numbers(Inverted 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 the input value till 1 i.e. use post decrementation 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 i, its value will last till 1 (j>=i),use post decrementation 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Â
8.In this manner the Inverted half 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=3, and it also satisfies the condition i.e. i>=1 hence it will get decremented by 1 i.e. i--
next loop,
j=i, satisfies the condition j>=1 i.e j>=3
it will print 3 3 3
then, it wiil go to next line "\n"
now,
i=2, satisfies condition i>=1
as 'j' was post decremented now,
j=2, satisfies condition j>=1 i.e. j>=2
it will print 2 2 Â
the it will go to next line "\n"
now,
i=1, satisfies condition i>=1
as 'j' was get post incremented now,
j=3, satisfies condition j>=1 i.e.j>=1
it will print 1
then it will go to next line "\n"
now,
i=0, but it does not satisfies the condition i>=1, therefore the execution of program will be stopped.
the Output is:-
3 2 1
2 1
1
C
/*
level 1.2
54321
4321
321
21
1
*/
#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);
// First for loop for rows management
for(i=n;i>=1;i--)
{
// Second for loop for columns management
for(j=i;j>=1;j--)
{
// printing the pattern
printf("%d",j);
}
// printing the code for next line
printf("\n");
}
return 0;
}
C++
//**HELPFORCODERS**
//This is a Pattern project which prints
//54321
//4321
//321
//21
//1
#include<iostream>
using namespace std;
//This is the main Function
int main()
{
cout<<"This is the Program to print the Pattern of INVERTED 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=row;i>=1; i--)
{
//Second for loop
for(j=i;j>=1;j--)
{
//Printing the pattern with spaces
cout<<j<<" ";
}
//This command helps to move to next line after printing the number
cout<<endl;
}
return 0;
}
Java
package com.company;
import java.util.*;
// Compiler version JDK 11.0.2
/*
level 1.2
54321
4321
321
21
1
*/
class pattern1_12
{
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 required pattern\n");
for (int i = n; i >= 1; i--){
for (int j = i; j >= 1; j--){
System.out.print(j);
}
System.out.println("");
}
}
}
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(n,0,-1):
# Second loop for no. of Columns
for j in range(i,0,-1):
# printing the value
print(j,end="")
# (\r)=End of the row
print("\r")
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP