Triangular Numbers | C ++ program to check if a number is a triangular number using simple approach . ; Returns true if ' num ' is triangular , else false ; Base case ; A Triangular number must be sum of first n natural numbers ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isTriangular ( int num ) { if ( num < 0 ) return false ; int sum = 0 ; for ( int n = 1 ; sum <= num ; n ++ ) { sum = sum + n ; if ( sum == num ) return true ; } return false ; } int main ( ) { int n = 55 ; if ( isTriangular ( n ) ) cout << " The ▁ number ▁ is ▁ a ▁ triangular ▁ number " ; else cout << " The ▁ number ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ; return 0 ; }