Haru
Haru

Reputation: 25

How to print the result in descending order in java?

I managed to get the result if I enter the base and exponent, but the output should be

For example: the output should look like this

>>base:5 exponent: 2

5^2 = 25
5^1 = 5

I need help to put something somewhere to make this happen...

import java.util.Scanner;

public class recursion {

    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int base = 0;
        int expo = 0;
    
        System.out.print("Enter number for base ");
        for (int i = 0; i < 1; i++)
            base = scanner.nextInt();
 
        System.out.print("Enter number for exponent ");
        for (int j = 0; j < 1; j++)
            expo = scanner.nextInt();

        System.out.println(base + "^" +expo +" = " +  pow(base,expo));

    }

    
    public static int pow(int x, int p) {

        System.out.println(x + "^" +p +" = " );
        if (p == 0)
            return 1;
        if (p % 2 == 0) {
            int a = pow(x, (p / 2));
            return  a * a; // This line
        } else {
            int a = pow(x, ((p - 1) / 2));
            return x * a * a; // This line
        }

    }

}

Upvotes: 1

Views: 1161

Answers (2)

Diviyo
Diviyo

Reputation: 57

void power(int base, int exp){

//Use for loop to iterate through each exp down to 0
for(int i=exp; i>=0; i--){
     int result= exponent(base,i);
     System.out.println(base + "^" + i + "=" + result)//Will display result as 5^2=25
}

//Recursively computes the result of b^e.

int exponent(int b, int e){
 if(e==0){//Base case occurs when e=0.
    return (1);
  }
return (b * exponent(b,e-1));
}

Upvotes: 0

Unihedron
Unihedron

Reputation: 11041

Firstly, the following code snippets demands a review:

{
    for (int i = 0; i < 1; i++)
    /*
     * This for loop is unnecessary.
     * It asserts that the following clause is run only once,
     * which is true for any statements anyway.
     */
        // ...
}

    return a * a;
} /* if (p % 2 == 0) */ else {
    /*
     * Statements are unnecessarily nested within else clause.
     * The corresponding then clause does not complete normally.
     * Moving the following code snippet out of an else block
     * would have the same effect, but simplifies the control
     * statements.
     */
    int a = pow(x, ((p - 1) / 2));
    return x * a * a;
}

Within your pow() method, you have a System.out.println() method. You're calling it for debugging, but it's unnecessary as the process returns normally. As you're looking for printing the operations for exponent as "from user-specified exponent -> 1" ("in descending order"), use a loop to print your System.out.println(base + "^" + expo + " = " + TestClass.pow(base, expo));:

    do // New!
        System.out.println(base + "^" + expo + " = " + TestClass.pow(base, expo));
    while (expo-- > 1); // New!

} /* main( args ) */

and you can remove the debugging line in pow().

Example: (>> denotes STDIN)

Enter number for base >> 5
Enter number for exponent >> 2
5^2 = 25
5^1 = 5

Enter number for base >> 4
Enter number for exponent >> 5
4^5 = 1024
4^4 = 256
4^3 = 64
4^2 = 16
4^1 = 4

View a live code demo.

Upvotes: 1

Related Questions