Program C ++ untuk Menghitung Kekuatan Angka

0
(0)

Program ini mengambil dua angka dari pengguna (angka dasar dan eksponen) dan menghitung daya.

Power of a number = baseexponent

Contoh 1: Hitung Daya Secara Manual

#include 
using namespace std;

int main() 
{
    int exponent;
    float base, result = 1;

    cout << "Enter base and exponent respectively:  ";
    cin >> base >> exponent;

    cout << base << "^" << exponent << " = ";

    while (exponent != 0) {
        result *= base;
        --exponent;
    }

    cout << result;
    
    return 0;
}

Keluaran

Enter base and exponent respectively:  3.4
5
3.4^5 = 454.354

Teknik di atas hanya berfungsi jika eksponen adalah bilangan bulat positif.

Jika Anda perlu menemukan kekuatan angka dengan bilangan real sebagai eksponen, Anda dapat menggunakannya pow() fungsi.


Contoh 2: Hitung daya menggunakan fungsi pow ()

#include 
#include 

using namespace std;

int main() 
{
    float base, exponent, result;

    cout << "Enter base and exponent respectively:  ";
    cin >> base >> exponent;

    result = pow(base, exponent);

    cout << base << "^" << exponent << " = " << result;
    
    return 0;
}

Keluaran

Enter base and exponent respectively:  2.3
4.5
2.3^4.5 = 42.44

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.