Oktal ke Desimal (Sebaliknya) dalam Bahasa C++
Hasil akhir :
Source Kode :
#include <iostream> #include <cmath> using namespace std; int desimal_oktal(int n); int oktal_desimal(int n); int main() { int n; char c; awal: cout << "\tPilihan : " << endl; cout << "\t---------------------------"; cout << "\n\t1. Desimal ke Oktal." << endl; cout << "\n\t2. Oktal ke Desimal" << endl; cout << "\n\tTentukan pilihan anda = "; cin >> c; if (c =='1') cout << "\n\tMasukan angka desimal nya = "; cin >> n; cout <<"\n\t"<< n << " Desimal = " << desimal_oktal(n) << " Oktal"; }else if (c =='2') { cout << "\n\tMasukan angka oktal nya = "; cin >> n; cout <<"\n\t"<< n << " Oktal = " << oktal_desimal(n) << " Desimal "; }else { cout <<"\n\tPilihan anda tidak ada"; } char yn; cout <<"\n\n\tApakah anda ingin mengulang program [Y/N] = "; cin >> yn; cout << "\n\n"; if (yn == 'y' || yn == 'Y') {goto awal;} if (yn == 'n' || yn == 'N') {goto end;} end: cout <<"\n\tTerima Kasih"; return 0; } int desimal_oktal(int n) /* Fungsi konversi desimal ke oktal */ { int rem, i=1, oktal=0; while (n!=0) { rem=n%8; n/=8; oktal+=rem*i; i*=10; } return oktal; } int oktal_desimal(int n) /* Fungsi konversi oktal ke desimal */ { int desimal=0, i=0, rem; while (n!=0) { rem = n%10; n/=10; desimal += rem*pow(8,i); ++i; } return desimal; }
0 komentar: