

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ code to check triangular number
Suppose we have a number n. We have to check whether the number is triangular number or not. As we know, if n dots (or balls) can be arranged in layers to form a equilateral triangle then n is a triangular number.
So, if the input is like n = 10, then the output will be True.
Steps
To solve this, we will follow these steps −
for initialize i := 1, when i <= n, update (increase i by 1), do: if i * (i + 1) is same as 2 * n, then: return true return false
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(int n){ for (int i = 1; i <= n; i++){ if (i * (i + 1) == 2 * n){ return true; } } return false; } int main(){ int n = 10; cout << solve(n) << endl; }
Input
10
Output
1
- Related Questions & Answers
- Python Program for Triangular Matchstick Number
- Finding whether a number is triangular number in JavaScript
- C/C++ Program for Triangular Matchstick Number?
- Program to check if matrix is lower triangular in C++
- Program to check if matrix is upper triangular in C++
- 8085 code to convert binary number to ASCII code
- C++ code to check phone number can be formed from numeric string
- C/C++ Program for the Triangular Matchstick Number?
- First triangular number whose number of divisors exceeds N in C++
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- What are forward triangular merger and reverse triangular merger?
- C++ code to check query for 0 sum
- Check for Valid hex code - JavaScript
- Program to print Lower triangular and Upper triangular matrix of an array in C
- How to check the website status code using PowerShell?
Advertisements