Knowee
Questions
Features
Study Tools

Write a C program to print the Pyramid with numbersWrite a program to print a pyramid of numbers separated by spaces for the given number of rows.At the time of execution, the program should print the message on the console as:Enter number of rows : For example, if the user gives the input as:Enter number of rows : 5then the program should print the result as:Enter number of rows : 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Question

Write a C program to print the Pyramid with numbersWrite a program to print a pyramid of numbers separated by spaces for the given number of rows.At the time of execution, the program should print the message on the console as:Enter number of rows : For example, if the user gives the input as:Enter number of rows : 5then the program should print the result as:Enter number of rows : 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a simple C program that prints a pyramid of numbers:

#include <stdio.h>

int main() {
    int rows, i, j, count = 1, space;

    printf("Enter number of rows : ");
    scanf("%d", &rows);

    for(i = 1; i <= rows; i++) {
        // Print spaces
        for(space = 1; space <= rows-i; space++) {
            printf("  ");
        }

        // Print numbers
        for(j = 1; j <= i; j++) {
            printf("%2d ", count++);
        }

        printf("\n");
    }

    return 0;
}

This program works by first asking the user to input the number of rows for the pyramid. It then uses two nested for loops to print the pyramid. The outer loop runs for each row of the pyramid, and the inner loops print the spaces and numbers for each row. The number of spaces before the numbers decreases with each row, while the number of numbers increases. The count variable is used to keep track of the current number to be printed.

This problem has been solved

Similar Questions

Develop a C program to print the following pattern of N rows using whileloop (Read N value from the Keyboard)For Example: N value : 512 34 5 67 8 9 1011 12 13 14

Write a program to print the following output: [3] 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1

#include<stdio.h> int main() { int rows,i,j,count=1,space; printf("Enter number of rows : "); scanf("%d",&rows); for(i=1;i<=rows;i++) { for(space=1;space<=rows-i;space++); { printf(" "); } for(j=1;j<=1;j++) {printf("%2d",count++);} printf(" "); } return 0; }

Design a Python script that uses a for loop to print a pattern of numbers or characters. For instance, create ascript that prints a pyramid pattern using asterisks (*) based on a user-input height. (15 Marks)

The output of the code will be ____. int i=5; printf(“%d %d %d”, i, i++, ++i); ans.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.