How to print the result in descending order in java?

Haru

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
        }

    }

}
Unihedron

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i print in descending order?

From Dev

How to sort the atoms in descending order and print it out

From Dev

How to make this Perl program print in descending order?

From Dev

Java: How do I print out in descending order the values of a list of objects' properties?

From Dev

Java: How do I print out in descending order the values of a list of objects' properties?

From Dev

How to print a descending/ascending number of spaces in Java?

From Dev

libxml2: How to sort xmlNode result in ascending and descending order?

From Dev

libxml2: How to sort xmlNode result in ascending and descending order?

From Dev

how to order ascending descending?

From Dev

How to order by case, descending?

From Dev

How to print even numbers in ascending order and odd numbers in descending order without using collection

From Dev

Confused on how this line of Java code sorts in descending order

From Dev

print map values in descending order in c++

From Dev

Array in Descending Reverse order in Java

From Dev

How to sort in descending order with numpy?

From Dev

How do I sort and print the phrases along the rating given in descending order

From Dev

how to use awk command to find all 5 length files - and print in descending order

From Dev

Similar to battleships. How do i print descending order with a border like so?

From Dev

MongoDB-PHP: How to sort the result of find query by timestamp in ascending/descending order?

From Dev

MySQL query resultset order by most number of result/matches in descending order

From Dev

how to make threads print in order java

From Dev

Java Multithreading - How to print numbers in natural order

From Dev

Sort map in descending order java8

From Dev

java - Sorting in Descending order using comparator

From Dev

Print (in descending order) the number occurrence in a 2D array

From Dev

Grouping Result Min/Max in a breaking series ascending/descending order in SQL

From Dev

Grouping Result Min/Max in a breaking series ascending/descending order in SQL

From Java

How to sort by column in descending order in Spark SQL?

From Dev

How to sort the string number in descending order?

Related Related

  1. 1

    How can i print in descending order?

  2. 2

    How to sort the atoms in descending order and print it out

  3. 3

    How to make this Perl program print in descending order?

  4. 4

    Java: How do I print out in descending order the values of a list of objects' properties?

  5. 5

    Java: How do I print out in descending order the values of a list of objects' properties?

  6. 6

    How to print a descending/ascending number of spaces in Java?

  7. 7

    libxml2: How to sort xmlNode result in ascending and descending order?

  8. 8

    libxml2: How to sort xmlNode result in ascending and descending order?

  9. 9

    how to order ascending descending?

  10. 10

    How to order by case, descending?

  11. 11

    How to print even numbers in ascending order and odd numbers in descending order without using collection

  12. 12

    Confused on how this line of Java code sorts in descending order

  13. 13

    print map values in descending order in c++

  14. 14

    Array in Descending Reverse order in Java

  15. 15

    How to sort in descending order with numpy?

  16. 16

    How do I sort and print the phrases along the rating given in descending order

  17. 17

    how to use awk command to find all 5 length files - and print in descending order

  18. 18

    Similar to battleships. How do i print descending order with a border like so?

  19. 19

    MongoDB-PHP: How to sort the result of find query by timestamp in ascending/descending order?

  20. 20

    MySQL query resultset order by most number of result/matches in descending order

  21. 21

    how to make threads print in order java

  22. 22

    Java Multithreading - How to print numbers in natural order

  23. 23

    Sort map in descending order java8

  24. 24

    java - Sorting in Descending order using comparator

  25. 25

    Print (in descending order) the number occurrence in a 2D array

  26. 26

    Grouping Result Min/Max in a breaking series ascending/descending order in SQL

  27. 27

    Grouping Result Min/Max in a breaking series ascending/descending order in SQL

  28. 28

    How to sort by column in descending order in Spark SQL?

  29. 29

    How to sort the string number in descending order?

HotTag

Archive