Make your own free website on Tripod.com
 

C PROGRAM:

Divisors

 

Home
About Us
Discussions
Quizzes
Reflections
Flowcharts
C Programs
Feedback

 

Here is a sample Turbo C program that gives the number, sum and product of divisors of a number given by the user. For expert programmers, you may need to do some adjustments if you are using another language apart from C. For those who do not understand programming, just click on the link at the bottom of this page and try the finished program out for yourself.

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#define p printf
#define s scanf
int ctr,cc,dd,ii,x,n,nmod,z,a[29436],b,k,g;
double numdiv,sumdiv,prodiv;
char cont;
main()
{

do
{  clrscr();
   numdiv=1;sumdiv=1;
   for (b=0;b<29436;b++) a[b]=0;
   p ("Input a number: ");
   s ("%d",&n);
   x=n;
   ctr=2;
   do
   {   nmod=n%ctr;
       if (nmod==0)
       {   a[ctr]+=1;
           n=n/ctr;
           ctr=2;
       }
   else if (nmod!=0) ctr+=1;
   }
   while (n!=1);
   for (cc=x;cc>0;cc--)

       if (a[cc]!=0) numdiv=numdiv*(a[cc]  +1);
   p ("\nThe number of divisors is %0.0lf",numdiv);
   dd=x-1;
   for (dd=x;dd>0;dd--)

       if (a[dd]!=0)

           sumdiv=sumdiv*(((pow(dd,(a[dd]+1)))-1)/(dd-1));
   p ("\nThe sum of the divisors is %0.0lf",sumdiv);
   ii=numdiv;
   k=ii/2;g=ii%2;
   if (g!=0)
   {   z=sqrt(x);
       prodiv=pow(z,numdiv);
       p ("\nThe product of the divisors in numerical form

         is %0.0lf",prodiv);
       p ("\nThe product of the divisors in exponential 

         form is %d to the power of %d",z,ii);
   }
   else
   {   prodiv=pow(x,k);
       p ("\nThe product of the divisors in numerical form

         is %0.0lf",prodiv);
       p ("\nThe product of the divisors in exponential

         form is %d to the power of %d",x,k);
   }
   p ("\n\nDo you wish to continue [Y/N]? ");
   cont=getche();
   }
while (cont=='Y'||cont=='y');
}

 


CLICK TO SEE ACTUAL PROGRAM (.zip)

Home | C PROGRAM: Determining Prime Numbers | C PROGRAM: Prime Factorization | C PROGRAM: Divisors

 

Copyright 2006. All rights reserved.

No part of this site may be reproduced without the permission of the authors.