Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.shpp.p2p.cs.dnapriushkin.assignment3;
- import com.shpp.cs.a.console.TextProgram;
- public class Assignment3Part3 extends TextProgram {
- /**
- * Condition: Write a program that raises numbers to a power
- * Result: The user sets different numbers that the program considers
- */
- public void run() {
- double base = 2; // Enter the number you want to raise to the power
- int exponent = 3; //Enter the degree to which you want to raise numbers
- raiseToPower(base, exponent);
- }
- //check number exponent on 0 and 1. we make calculations according to the given numbers
- public int raiseToPower(double base, int exponent) {
- if (exponent == 0) { // check for 0
- return 1;
- }
- if (exponent == 1) {
- int result = (int) base;
- }
- int result = 1;
- if (exponent > 1) {
- for (int i = 0; i < exponent; i++) { // create a cycle and perform calculations if the degree is positive
- result = (int) (result * base);
- }
- } else if (exponent < 0) {
- throw new IllegalArgumentException("Error: Exponent Must Be >=0");
- }
- println(result + " result ");
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement