#include <iostream>
using namespace std;
int duaLog_n(int x, int n);
int banyak_kali_duaLog_n(int x, int n);
int i = 0;
int main()
{
int x, n;
cout << "Program menghitung nilai 2 pangkat n " << endl;
cout << "-------------------------------------" << endl;
cout << endl;
cout << "masukkan nilai x : "; cin >> x;
cout << "masukkan nilai n : "; cin >> n;
cout << "hasil " << x << "^" << n << " adalah = " << duaLog_n(x, n) << endl;
cout << "banyak perkalian : " << banyak_kali_duaLog_n(x,n);
cout << endl;
}
int duaLog_n(int x, int n)
{
int i=0,y, hasil;
if (n==1)
{
hasil = x;
i=0;
} else if (n>1)
{
y = duaLog_n(x,(n/2));
if (n%2 == 1)
{
hasil = y*y*x;
} else
{
hasil = y*y;
}
}
return (hasil);
}
int banyak_kali_duaLog_n(int x, int n)
{
int y, hasil;
if (n==1)
{
hasil = x;
i=0;
} else if (n>1)
{
y = banyak_kali_duaLog_n(x,(n/2));
if (n%2 == 1)
{
i+=2;
} else
{
i++;
}
}
return (i);
}
Tampilan Hasil |
0 komentar:
Posting Komentar