Printing all divisors of a number in c++

 Printing divisor in c++

I wrote the program to print all the divisors of a number...
#include<iostream>
#include<cmath>

int divisor(int n)
{
    int i;
    for(i = 1; i<= sqrt(n); i++)
    {
        if(n%i==0)
        {
            std::cout<<i<<" ";
        }
    }
   
    for( ; i>=1; i--)
    {
        if(n%i==0)
        {
            std::cout<<(n/i)<<" ";
        }
    }
}

int main(){
    std::cout<<"Enter a number to print it's divisors: "<<std::endl;
    int number;
    std::cin>>number;
    std::cout<<"Here are it's divisors:"<<std::endl;
    divisor(number);
    return 0;
}

Post a Comment

0 Comments