program perpangkatan menggunakan algoritma DnC


   program perpangkatan

#include <cstdlib>
#include <iostream>

using namespace std;

bool odd(int n){
              if(n%2==1)
              return true;
              return false;
}
float exp2(float a,int n){
      double x;
    
      if(n==0){
               return 1;
                   }
          else{
           x=exp2(a,n/2);   
              if(odd(n)){
                 return x*x*a;
                 }
           else{
                return x*x;
                }
           }
           }
int main(int argc, char *argv[])
{
    int a,n;
    cout<<"hasilnya adalah:"<<exp2(2,9)<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

0 komentar:

Posting Komentar

Copyright 2009 my dear. All rights reserved.
Bread Machine Reviews | watch free movies online by Blogger Templates