Thursday, September 30, 2010

Program to print prime factorization

// Program to print prime factorization

#include<conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
main()
    {
    clrscr();
    char line[512]; /* input string */
    short ii;   /* number to be factored */
    short j;   /* divisors */
    short p;   /* powers of j in ii */
    cout<<"Please enter the number to be factored: ";
    cin.getline(line, 512);
    ii = atoi(line);
    cout<<"The prime divisors of "<< ii<< " are/ is ";
    for (j = 2; ii != 1; j = j + 1)
{
if (ii % j == 0)
   {
   for (p = 0; ii % j == 0; p = p + 1)
ii = ii / j;
   cout<< j<<" ";
   }
}
    cout<<endl;
    getch();
    }

No comments:

Post a Comment