
- 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 1 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 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.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; void findDivisors(int n) { for (int i = 1; i <= n; i++) { if (n % i == 0) { cout << i << " "; } } cout << endl; } int main() { findDivisors(65); return 0; }
Output
If you run the above code, then you will get the following result.
1 5 13 65
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 2 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
- Find the number of divisors of all numbers in the range [1, n] in C++
- Count all perfect divisors of a number 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 the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
- Find largest sum of digits in all divisors of n in C++
- Divisors of factorials of a number in java
- 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++
- Find the number of all three digit natural numbers which are divisible by 9.

Advertisements