[ back to toc ]

C

Date: 2002/03/06 12:00

Q:
hi,i wrote this program that doesnt work!!
can you correct it for me pleaseeeeeee..
the question is::
A-write a function that takes an integer(assume it is not equal to 1) and
return its smallest divisor other than 1,(for 4 return 2..)
b-use this function to write a recursive function that takes an integer
other than 1 and print on the screen its prime factorization.(for 8 it
prints 2*2*2,for 10 it prints 2*5)

***********************************************
this is the program which i have written::
#include<stdio.h>
/////////////////////////////////////////////////////

int divisor (int n)
{
int i=2;
while(i<=n)
{ if(n%i==0)
return i;
else i++;
}
}
//////////////////////////////////////////////////////
int fact (int x)
{ int m,last;
if(x==1)return 0 ;
if(last!=1)
m=divisor(x);
printf("%i",m);
last=x/m;
printf(" * ");
return fact(last);
}
////////////////////////////////////////////////////////
int main()
{
int x,compute;
printf("Enter an integer");
scanf(" %i ",x);
compute=fact(x);
return 0;
}

A:
int fact (int x)
{ int m,last;
if(x==1)return 0 ;
if(last!=1) <--- last here does not have any value!
m=divisor(x);
printf("%i",m);
last=x/m;
printf(" * ");
return fact(last);
}

Regards,
Peter

[ back to toc ]