- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
First triangular number whose number of divisors exceeds N in C++
In this tutorial, we are going to find a triangular number whose number of divisors are greater than n.
If the sum of natural numbers at any point less than or equal to n is equal to the given number, then the given number is a triangular number.
We have seen what triangular number is. Let's see the steps to solve the problem.
Initialize the number
Write a loop until we find the number that satisfies the given conditions.
Check whether the number is triangular or not.
Check whether the number has more than n divisors or not.
If the above two conditions are satisfied then print the number and break the loop.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; bool isTriangular(int n) { if (n < 0) { return false; } int sum = 0; for (int i = 1; sum <= n; i++) { sum += i; if (sum == n) { return true; } } return false; } int divisiorsCount(int n) { int count = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { count += 1; } } return count; } int main() { int n = 2, i = 1; while (true) { if (isTriangular(i) && divisiorsCount(i) > 2) { cout << i << endl; break; } i += 1; } return 0; }
Output
If you run the above code, then you will get the following result.
6
Conclusion
If you have any queries in the tutorial, mention them in the comment section.