
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
- Related Articles
- Find the largest good number in the divisors of given number N in C++
- Minimum number of Square Free Divisors in C++
- Find the number of divisors of all numbers in the range [1, n] in C++
- Find sum of divisors of all the divisors of a natural number in C++
- Count the numbers < N which have equal number of divisors as K in C++
- Find number from its divisors in C++
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Count all perfect divisors of a number in C++
- Minimum number of squares whose sum equals to given number n\n
- Divisors of n-square that are not divisors of n in C++ Program
- n-th number whose sum of digits is ten in C++
- Find M-th number whose repeated sum of digits of a number is N in C++
- C++ code to check triangular number
- C/C++ Program for Triangular Matchstick Number?
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
