/* MCS 275 L-2 Wed 16 Jan 2008 : facnum2.c
 * factor a number into primes,
 * we use a function to factor */

#include <stdio.h>
#include <string.h>

int factor ( int n, char *s );
/* writes the prime factors of n
 * into the string s */

int main ( void )
{
   int n;
   char s[80]; /* string for result */

   printf("give a natural number n : ");
   scanf("%d",&n);

   factor(n,s);

   printf(s); /* print result */

   return 0;
}

int factor ( int n, char *s )
{
   int d,r;
   char f[10]; /* string for factor */

   sprintf(s,"%d =",n);
   d = 2;
   while(d < n)
   {
      r = n % d;
      if(r == 0)
      {
         sprintf(f," %d",d);
         strcat(s,f);
         n = n/d;
         d = 2;
      }
      else
         d = d + 1;
   }
   sprintf(f," %d\n",n);
   strcat(s,f);

   return 0;
}

