- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- How to Check Whether a Number is a Triangular Number or Not in Java?
- C++ code to check phone number can be formed from numeric string
- 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++
- C/C++ Program for the Triangular Matchstick Number?
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- C++ code to check query for 0 sum
- First triangular number whose number of divisors exceeds N in C++
- C++ code to check string is diverse or not
- C++ code to check review vote status and uncertainty
- Find the fifth triangular number.
- C++ code to count number of unread chapters
- C++ code to check pattern is center-symmetrical or not
- C++ code to check given matrix is good or not

Advertisements