C Program to Check Whether a Number is Triangular or Not
This C program checks whether a given number by user is Triangular number or not. 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
#include<stdio.h>
#include<conio.h>
int main()
{
int number, i, sum=0;
clrscr();
printf("Enter number: ");
scanf("%d", &number);
for(i=0;i < number; i++)
{
sum = sum+i;
if(sum == number)
{
printf("%d is TRIANGULAR NUMBER.", number);
break;
}
}
if(number == i)
{
printf("%d is NOT TRIANGULAR NUMBER.", number);
}
getch();
return(0);
}
Output of the above program :
Run 1: ------------ Enter number: 15 ↲ 15 is TRIANGULAR NUMBER. Run 2: ------------ Enter number: 155 ↲ 155 is NOT TRIANGULAR NUMBER. Run 3: ------------ Enter number: 153 ↲ 153 is TRIANGULAR NUMBER. Run 4: ------------ Enter number: 276 ↲ 276 is TRIANGULAR NUMBER. Note: ↲ indicates enter is pressed.