Calculating the nth Triangular Number - C Function

C examples for Function:Function Definition

Description

Calculating the nth Triangular Number

Demo Code

#include <stdio.h>

void calculateTriangularNumber (int n){
    int i, triangularNumber = 0;

    for (i = 1; i <= n; ++i)
        triangularNumber += i;// w  w  w.  j a  va  2s  . co m

    printf("Triangular number %i is %i\n", n, triangularNumber);
}

int main (void)
{
    calculateTriangularNumber (10);
    calculateTriangularNumber (20);
    calculateTriangularNumber (50);

    return 0;
}

Result


Related Tutorials