C Program to Check Whether a Given Number is Triangular Number or Not

Program


#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.