C++ Program to find all the divisor of a given number














































C++ Program to find all the divisor of a given number



Description :
Given a natural number n, print all distinct divisors of it.

C++ Program to find all the divisor of a given number

#include <bits/stdc++.h>
using namespace std ; int main() { int n , i; cout<<" Enter the number to find its divisor \n"; cin>>n; cout<<" All the divisors of "<< n <<" are "<< endl; for( i=1 ; i <= n ; i++) { if( n % i == 0) { cout<< i <<" "; } } return 0; }

Output :
Enter the number to find its divisor
10 All the divisors of 10 are 1 2 5 10

Time Complexity : O(n) 
Auxiliary Space : O(1)

Comments