Advertisement
agusbd

faktorisasprima

Oct 30th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int jumlah=0;
  5.  
  6. bool prima(int x)
  7. {  
  8.    int jumlah = 0;
  9.    for(int i =1;i<=x;i++)
  10.    {
  11.        if(x%i==0)
  12.         jumlah++;
  13.        if(jumlah>2)
  14.         break;
  15.    }
  16.    if(jumlah>2)
  17.      return false;
  18.    else
  19.     return true;
  20. }
  21.  
  22. int hitung(int x)
  23. {
  24.     if(x==1)
  25.     {
  26.         return 0;
  27.     }
  28.     for(int i =2; i<=x;i++)
  29.     {   jumlah=0;
  30.         if(prima(i))
  31.         {
  32.             if(x%i==0)
  33.             {
  34.             while(x%i==0)
  35.             {
  36.             jumlah++;
  37.             x=x/i;
  38.             }
  39.             cout<<i<<"^"<<jumlah;
  40.             if(x!=1)
  41.             cout<<" x ";
  42.             return hitung(x);
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48. main()
  49. {
  50.     int num,jumlah;
  51.     int sisa;
  52.     cout<<"masukan angka : ";cin>>num;
  53.     cout<<"faktorisasi primanya adalah : ";
  54.     hitung(num);
  55.  
  56.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement