Generating a table of Triangular Numbers, output as a table - C Statement

C examples for Statement:for

Description

Generating a table of Triangular Numbers, output as a table

Demo Code

#include <stdio.h>

int main (void)
{
    int n, triangularNumber;

    printf ("TABLE OF TRIANGULAR NUMBERS\n\n");
    printf (" n    Sum from 1 to n\n");
    printf ("---   ---------------\n");

    triangularNumber = 0;/*from w w  w. j  a  v a 2  s.  c o m*/

    for (n = 1; n <= 10; ++n)
    {
        triangularNumber += n;
        printf ("%2i          %i\n", n, triangularNumber);
    }

    return 0;
}

Result


Related Tutorials