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.

How to print numbers in triangle shape using c??

+11 votes
asked Feb 5, 2023 by Naveenreddy (260 points)

2 Answers

0 votes
answered Feb 6, 2023 by Peter Minarik (82,920 points)
Please, write your code, make your best attempt to achieve your goal and share it. We'll be more than happy to help.

However, it is your homework, others are not supposed to write it for you.
commented Feb 10, 2023 by Michael Paul Harris (100 points)
It's code; we all copy. That is how we learn. You cannot expect beginners to magically know the intricacies of a language without seeing how functions are executed. That is the same as giving a person a hammer and wood and telling them- "learn how to build beautiful cabinets." As programmers, you copy code when you learn. You did not just practice code and magically obtain the knowledge. That's not how learning code is done, initially for beginners.
commented Feb 12, 2023 by Peter Minarik (82,920 points)
Sorry, I disagree.

I expect people to have a loop and print something on the screen. It may not be a triangle or the code may not compile, but at least they show they made an effort to find out what function is used to print something on the screen or how to create a variable.

Google how we learn and the vast majority of your hits will tell you the most you learn from your own efforts, when you try to remember and understand things. If you just copy someone else's solution without any effort, you won't learn anything.

So yeah, let people do a tutorial, have a few lines of code as an effort before they ask for a complete solution.
0 votes
answered Feb 7, 2023 by Florian Herfurth (140 points)
First of all, please do not just copy paste the code. Try to understand it. You can do this to write numbers in a triangle shape;

#include <stdio.h>

int main()
{
    int i, j, rows;

    // User input the number of rows
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Iterate through the rows
    for (i = 0; i < rows; i++)
    {
        // Print numbers in each row
        for (j = 0; j <= i; j++)
        {
            printf("%d ", j);
        }
        // Print new line after each row
        printf("\n");
    }

    return 0;
}
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.
...