In deze sectie zullen we Java-programma's schrijven om de macht van een getal te bepalen. Om de macht van een getal te bepalen, vermenigvuldig je het getal met zijn exponent.
Voorbeeld:
Stel dat het grondtal 5 is en de exponent 4. Om de macht van een getal te bepalen, vermenigvuldig je het getal vier keer met zichzelf, d.w.z. (5 * 5 * 5 * 5 = 625).
Hoe bepaal je de macht van een getal?
- Grondtal en exponent moeten worden gelezen of geïnitialiseerd.
- Neem nog een variabele macht en stel deze in op 1 om de uitkomst op te slaan.
- Vermenigvuldig het grondtal met macht en sla het resultaat op in macht met behulp van de for- of while-lus.
- Herhaal stap 3 totdat de exponent gelijk is aan nul.
- Druk de uitvoer af.
Methoden om de macht van een getal te vinden
Er zijn verschillende methoden om de macht van een getal te bepalen:
array van strings in c-programmering
- Java for Loop gebruiken
- Java gebruiken tijdens Loop
- Recursie gebruiken
- Met behulp van de Math.pow()-methode
- Bitmanipulatie gebruiken
1. Java gebruiken voor Loop
Een for-lus kan worden gebruikt om de macht van een getal te berekenen door het grondtal herhaaldelijk met zichzelf te vermenigvuldigen.
PowerOfNumber1.java
public class PowerOfNumber1 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = 1; for (int i = 0; i <exponent; i++) { result *="base;" } system.out.println(base + ' raised to the power of exponent is result); < pre> <p> <strong>Output:</strong> </p> <pre> 2 raised to the power of 3 is 8 </pre> <h3>2. Using Java while Loop</h3> <p>A while loop may similarly be used to achieve the same result by multiplying the base many times.</p> <p> <strong>PowerOfNumber2.java</strong> </p> <pre> public class PowerOfNumber2 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = 1; int power=3; while (exponent > 0) { result *= base; exponent--; } System.out.println(base + ' raised to the power of ' + power + ' is ' + result); } } </pre> <p> <strong>Output:</strong> </p> <pre> 2 raised to the power of 3 is 8 </pre> <h3>3. Using Recursion:</h3> <p>Recursion is the process of breaking down an issue into smaller sub-problems. Here's an example of how recursion may be used to compute a number's power.</p> <p> <strong>PowerOfNumber3.java</strong> </p> <pre> public class PowerOfNumber3 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = power(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is ' + result); } public static int power(int base, int exponent) { if (exponent == 0) { return 1; } else { return base * power(base, exponent - 1); } } } </pre> <p> <strong>Output:</strong> </p> <pre> 2 raised to the power of 3 is 8 </pre> <h3>4. Using Math.pow() Method</h3> <p>The java.lang package's Math.pow() function computes the power of an integer directly.</p> <p> <strong>PowerOfNumber4.java</strong> </p> <pre> public class PowerOfNumber4 { public static void main(String[] args) { double base = 2.0; double exponent = 3.0; double result = Math.pow(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is ' + result); } } </pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 3.0 is 8.0 </pre> <h3>Handling Negative Exponents:</h3> <p>When dealing with negative exponents, the idea of reciprocal powers might be useful. For instance, x^(-n) equals 1/x^n. Here's an example of dealing with negative exponents.</p> <p> <strong>PowerOfNumber5.java</strong> </p> <pre> public class PowerOfNumber5 { public static void main(String[] args) { double base = 2.0; int exponent = -3; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { if (exponent >= 0) { return calculatePositivePower(base, exponent); } else { return 1.0 / calculatePositivePower(base, -exponent); } } static double calculatePositivePower(double base, int exponent) { double result = 1.0; for (int i = 0; i <exponent; i++) { result *="base;" } return result; < pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of -3 is: 0.125 </pre> <h3>Optimizing for Integer Exponents:</h3> <p>When dealing with integer exponents, you may optimize the calculation by iterating only as many times as the exponent value. It decreases the number of unneeded multiplications.</p> <p> <strong>PowerOfNumber6.java</strong> </p> <pre> public class PowerOfNumber6 { public static void main(String[] args) { double base = 2.0; int exponent = 4; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; for (int i = 0; i <exponent; i++) { result *="base;" } return result; < pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 4 is: 16.0 </pre> <h3>5. Using Bit Manipulation to Calculate Binary Exponents:</h3> <p>Bit manipulation can be used to better improve integer exponents. To do fewer multiplications, an exponent's binary representation might be used.</p> <p> <strong>PowerOfNumber7.java</strong> </p> <pre> public class PowerOfNumber7 { public static void main(String[] args) { double base = 2.0; int exponent = 5; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent > 0) { if ((exponent & 1) == 1) { result *= base; } base *= base; exponent >>= 1; } return result; } } </pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 5 is: 32.0 </pre> <hr></exponent;></pre></exponent;></pre></exponent;>
2. Java gebruiken tijdens Loop
Een while-lus kan op dezelfde manier worden gebruikt om hetzelfde resultaat te bereiken door de basis vele malen te vermenigvuldigen.
PowerOfNumber2.java
blokkeer YouTube-advertenties Android
public class PowerOfNumber2 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = 1; int power=3; while (exponent > 0) { result *= base; exponent--; } System.out.println(base + ' raised to the power of ' + power + ' is ' + result); } }
Uitgang:
2 raised to the power of 3 is 8
3. Recursie gebruiken:
Recursie is het proces waarbij een probleem wordt opgedeeld in kleinere deelproblemen. Hier is een voorbeeld van hoe recursie kan worden gebruikt om de macht van een getal te berekenen.
PowerOfNumber3.java
public class PowerOfNumber3 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = power(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is ' + result); } public static int power(int base, int exponent) { if (exponent == 0) { return 1; } else { return base * power(base, exponent - 1); } } }
Uitgang:
2 raised to the power of 3 is 8
4. Met behulp van de Math.pow()-methode
De functie Math.pow() van het java.lang-pakket berekent rechtstreeks de macht van een geheel getal.
die school maakte
PowerOfNumber4.java
public class PowerOfNumber4 { public static void main(String[] args) { double base = 2.0; double exponent = 3.0; double result = Math.pow(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is ' + result); } }
Uitgang:
2.0 raised to the power of 3.0 is 8.0
Omgaan met negatieve exponenten:
Bij het omgaan met negatieve exponenten kan het idee van wederkerige machten nuttig zijn. x^(-n) is bijvoorbeeld gelijk aan 1/x^n. Hier is een voorbeeld van het omgaan met negatieve exponenten.
PowerOfNumber5.java
public class PowerOfNumber5 { public static void main(String[] args) { double base = 2.0; int exponent = -3; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { if (exponent >= 0) { return calculatePositivePower(base, exponent); } else { return 1.0 / calculatePositivePower(base, -exponent); } } static double calculatePositivePower(double base, int exponent) { double result = 1.0; for (int i = 0; i <exponent; i++) { result *="base;" } return result; < pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of -3 is: 0.125 </pre> <h3>Optimizing for Integer Exponents:</h3> <p>When dealing with integer exponents, you may optimize the calculation by iterating only as many times as the exponent value. It decreases the number of unneeded multiplications.</p> <p> <strong>PowerOfNumber6.java</strong> </p> <pre> public class PowerOfNumber6 { public static void main(String[] args) { double base = 2.0; int exponent = 4; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; for (int i = 0; i <exponent; i++) { result *="base;" } return result; < pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 4 is: 16.0 </pre> <h3>5. Using Bit Manipulation to Calculate Binary Exponents:</h3> <p>Bit manipulation can be used to better improve integer exponents. To do fewer multiplications, an exponent's binary representation might be used.</p> <p> <strong>PowerOfNumber7.java</strong> </p> <pre> public class PowerOfNumber7 { public static void main(String[] args) { double base = 2.0; int exponent = 5; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent > 0) { if ((exponent & 1) == 1) { result *= base; } base *= base; exponent >>= 1; } return result; } } </pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 5 is: 32.0 </pre> <hr></exponent;></pre></exponent;>
Optimaliseren voor gehele exponenten:
Als u te maken heeft met gehele exponenten, kunt u de berekening optimaliseren door slechts zo vaak te itereren als de exponentwaarde. Het vermindert het aantal onnodige vermenigvuldigingen.
PowerOfNumber6.java
niet gelijk aan mysql
public class PowerOfNumber6 { public static void main(String[] args) { double base = 2.0; int exponent = 4; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; for (int i = 0; i <exponent; i++) { result *="base;" } return result; < pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 4 is: 16.0 </pre> <h3>5. Using Bit Manipulation to Calculate Binary Exponents:</h3> <p>Bit manipulation can be used to better improve integer exponents. To do fewer multiplications, an exponent's binary representation might be used.</p> <p> <strong>PowerOfNumber7.java</strong> </p> <pre> public class PowerOfNumber7 { public static void main(String[] args) { double base = 2.0; int exponent = 5; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent > 0) { if ((exponent & 1) == 1) { result *= base; } base *= base; exponent >>= 1; } return result; } } </pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 5 is: 32.0 </pre> <hr></exponent;>
5. Bitmanipulatie gebruiken om binaire exponenten te berekenen:
Bitmanipulatie kan worden gebruikt om gehele exponenten beter te verbeteren. Om minder vermenigvuldigingen uit te voeren, kan de binaire representatie van een exponent worden gebruikt.
PowerOfNumber7.java
public class PowerOfNumber7 { public static void main(String[] args) { double base = 2.0; int exponent = 5; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent > 0) { if ((exponent & 1) == 1) { result *= base; } base *= base; exponent >>= 1; } return result; } }
Uitgang:
2.0 raised to the power of 5 is: 32.0