[Strategi Algoritma] Program C++ Pangkat

function pangkat(a : real, n : integer) ® real

{ Menghitung a^n }

Deklarasi

  i : integer

  hasil : real

Algoritma:

  hasil -> 1

  for i ->1 to n do

     hasil -> hasil * a

  end

  return hasil

 

jika di implementasikan di program c++, maka

#include <cstdlib>
#include <iostream>

using namespace std;

float pangkat(float a, int n){
int i;
float hasil;

hasil=1;
for(i=1;i<=n;i++){
hasil=hasil*a;
}
return hasil;
}

int main(int argc, char *argv[])
{
int x,y;

cout<<“Masukkan angka : “;
cin>>x;
cout<<“Masukkan pangkat : “;
cin>>y;
cout<<“Hasil : “<<pangkat(x,y);
cout<<endl;
cout<<endl;

system(“PAUSE”);
return EXIT_SUCCESS;
}

 

Outputnya :

Image

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s