+92 332 4229 857 99ProjectIdeas@Gmail.com

How to calculate divisors of a number (C++)


Calculate divisors of a number
displayDivisors() displays all the divisors of the number. It takes the number as an input parameter and displays all the divisors of the given number.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

void displayDivisors(int num)
{
       for(int i=1;i<=num;i++)
       {
       if(num%i==0)
              cout<<i<<" "
       }
}

int main()
{
       cout<<"Divisors of    5 are : "; displayDivisors (    5 ) ;cout<<endl;
       cout<<"Divisors of    6 are : "; displayDivisors (    6 ) ;cout<<endl;
       cout<<"Divisors of   10 are : "; displayDivisors (   10 ) ;cout<<endl;
       cout<<"Divisors of   20 are : "; displayDivisors (   20 ) ;cout<<endl;
       cout<<"Divisors of   30 are : "; displayDivisors (   30 ) ;cout<<endl;
       cout<<"Divisors of   40 are : "; displayDivisors (   40 ) ;cout<<endl;
       cout<<"Divisors of   50 are : "; displayDivisors (   50 ) ;cout<<endl;
       cout<<"Divisors of   80 are : "; displayDivisors (   80 ) ;cout<<endl;
       cout<<"Divisors of   99 are : "; displayDivisors (     99 ) ;cout<<endl;
       cout<<"Divisors of  100 are : "; displayDivisors (  100 ) ;cout<<endl;

       _getche();
       return 0;
}

Output
Divisors of    5 are : 1 5
Divisors of    6 are : 1 2 3 6
Divisors of   10 are : 1 2 5 10
Divisors of   20 are : 1 2 4 5 10 20
Divisors of   30 are : 1 2 3 5 6 10 15 30
Divisors of   40 are : 1 2 4 5 8 10 20 40
Divisors of   50 are : 1 2 5 10 25 50
Divisors of   80 are : 1 2 4 5 8 10 16 20 40 80
Divisors of   99 are : 1 3 9 11 33 99
Divisors of  100 are : 1 2 4 5 10 20 25 50 100

0 comments: