C Program: Generating Triangular Numbers (Recursive Function)
This C program checks whether a given number by user is Triangular number or not uisng Recursive Function. Triangular Numbers are those numbers which are obtained by continued summation of the natural numbers 1, 2, 3, 4, 5, etc.
Triangular Number Example: 15 is Triangular Number because it can be obtained by 1+2+3+4+5+6 i.e. 1+2+3+4+5+6=15
List of Triangular Numbers: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666,
C Source Code: Check Triangular Number Using Recursion
#include<stdio.h>
#include<conio.h>
/* Function prototype */
int triangular(int n);
/* Main Function */
int main()
{
int term, i;
clrscr();
printf("How many terms?\n");
scanf("%d", &term);
printf("First %d triangular numbers are:\n", term);
for(i=1; i<=term; i++)
{
printf("%d\t", triangular(i));
}
getch();
return 0;
}
/* Function definition */
int triangular(int n)
{
if(n<=1)
{
return 1;
}
else
{
return n+triangular(n-1); /* Recursive Function */
}
}