Menghitung Hasil Perpangkatan C++

1. Raptor



2. C++



#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    int i;
    float a,x,y,hasil,pangkat=1;
    cout<<"Menghitung Hasil Perpangkatan"<<endl;
    cout<<"Masukan Bilang =";cin>>x;
    cout<<"Masukan Pangkat =";cin>>y;
    if (x>0 && y>0){
    for (i=1;i<=y;i++)
    pangkat*=x;
    cout<<x<<"^"<<y<<" = "<<pangkat;
    }else if (x>0 && y<0){
    a=y-y-y;
    for (i=1;i<=a;i++){
    pangkat*=x;
    }
            hasil=1/pangkat;
            cout<<x<<"^"<<y<<" = "<<hasil;
    }else {
        a=y-y-y;
    for (i=1;i<=a;i++){
    pangkat*=x;
    }
            hasil=1/pangkat;
            cout<<x<<"^"<<y<<" = "<<hasil;
    }
    return 0;
}

Komentar