
- 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
Find all divisors of a natural number - Set 2 in C++
In this tutorial, we are going to write a program that finds all the divisors of a natural number. It's a straightforward problem. Let's see the steps to solve it.
Initialize the number.
Write a loop that iterates from 1 to the square root of the given number.
Check whether the given number is divisible by the current number or not.
If the above condition satisfies, then print the current number and given_number/current_number.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; void findDivisors(int n) { for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) { cout << i << " "; } else { cout << i << " " << n / i << " "; } } } cout << endl; } int main() { findDivisors(65); return 0; }
Output
If you run the execute the above program, then you will get the following result.
1 65 5 13
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Find all divisors of a natural number - Set 1 in C++
- Find all divisors of a natural number in java
- Find sum of divisors of all the divisors of a natural number in C++
- Sum of all proper divisors of a natural number in java
- Count all perfect divisors of a number in C++
- Find the number of divisors of all numbers in the range [1, n] in C++
- Check if a number is divisible by all prime divisors of another number in C++
- Find number from its divisors in C++
- Find largest sum of digits in all divisors of n in C++
- Divisors of factorials of a number in java
- Find the sum of all 2-digit natural numbers divisible by 4.
- Find all palindromic sub-strings of a given string - Set 2 in Python
- Counting divisors of a number using JavaScript
- Sum of all subsets of a set formed by first n natural numbers
- Find the largest good number in the divisors of given number N in C++

Advertisements