Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed How to print this?

+9 votes
asked Sep 3, 2023 by Athul Ullas (210 points)
closed Sep 20, 2023 by Admin
5

4  5

3  4  5

2  3  4  5

1  2  3  4  5
closed with the note: answered

6 Answers

+2 votes
answered Sep 4, 2023 by Peter Minarik (82,920 points)

To solve this problem you could use nested loops: a loop inside a loop would do the trick for you.

The outer loop would go through the rows one by one, we could call the loop variable row.

The inner loop would go from N - row until it reaches N (N = 5 in your example) and print each number.

After the inner loop terminates, you can print a line break so the next iteration of the outer loop would start printing numbers in a new line.

Allow me to write some pseudo-code for you:

function: PrintNumberPyramid
arg: numRows

for row: [0, numRows)
    for digit: [numRows - row, numRows]
        print digit
    print '\n'

Note: [a, b] denotes a range with both left and right boundaries included while [a, b) denotes a range where the left boundary is included but the right boundary is excluded.

This should give you enough ideas to start coding. Share your solution when finished!

Good luck!

0 votes
answered Sep 8, 2023 by Ananya K.J (300 points)
#include <iostream>
using namespace std;
int main() {
    int n,i,j;
    cin>>n;
    for(i=0;i<n;i++)
    {
        for(j=0;j<=i;j++)
        {
            cout<<n-i+j;
        }
        cout<<endl;
    }

    return 0;
}
0 votes
answered Sep 8, 2023 by Praveen kumar Madan (190 points)
#include <stdio.h>

int main()
{
    int n;scanf("%d",&n);
    for(int i=0;i<n;i++,printf("\n"))for(int j=n;j>=n-i;j--)printf("%d ",j);
    return 0;
}
+1 vote
answered Sep 8, 2023 by SHUBHAM KUMAR (160 points)
#include<stdio.h>
int main()
{
    int i,j,n;
 
   
    printf("Enter how many rows you want : ");
    scanf("%d",&n);

    for(i=n;i>0;i--)
    {
        for(j=i;j<=n;j++)
            printf(" %d",j);
        
        printf("\n");
    }
 
    return 0;
}
+1 vote
answered Sep 16, 2023 by 218A1A05B8 NUSUM SASI PREETHAM (560 points)
#include<stdio.h>

void main(){

int i,j,a;

scanf("%d",&a);

for(i=1;i<=a;i++){

for(j=a;j>=a-i;j--){

printf("%d ",j);

}

printf("\n");

}

}
0 votes
answered Sep 17, 2023 by yakub (140 points)
// This is in C++
#include<iostream>
using namespace std;
int main(){
    int n;
    cout<<"enter the number of rows : "<<endl;
    cin>>n;
    for(int i=n;i>=1;i--){
        for(int j=i;j<=n;j++){cout<<" "<<j;}
        cout<<endl; }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...