user12929063
user12929063

Reputation:

How to use nested loops to output powers of integers and their expanded form, respectively

I want to output the power table from n (0) to n (10) with just the base being inputted using the scanner.

For now, I am having difficulties in setting it up

Code:

else if (option == 2){
                int base = keyboard.nextInt();
                for (int x = base; x <= base; x++){
                    System.out.print(base+"^");
                    for (int y = 0; y <= 10; y++){ // "y" is exponent
                        System.out.print(y+"=");
                }
                System.out.println("");
            }
        }

Sample input:

  2
  5

Expected Output:

     5^0=
     5^1=
     5^2=
     5^3=
     - - - several lines are skipped here - - -
     5^10=

Note: This is not the expected output, but I want to try out the code by myself this is just the step that would take me to the end result

Upvotes: 2

Views: 501

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78955

Problems in your code:

  1. You have used result inside the loop and therefore it is getting reset with each iteration.
  2. You have put the print statement inside the loop
  3. You need to multiply result with base with each iteration which is not done.

Do it as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int option = keyboard.nextInt();
        int base = keyboard.nextInt();
        int exponent = keyboard.nextInt();
        int result = 1;
        if (option == 1) {
            for (int x = base; x <= base; x++) {
                if (exponent <= 1) {
                    System.out.print(base + "^" + exponent);
                } else {
                    System.out.print(base + "^" + exponent + "=");
                }
                for (int y = 1; y <= exponent; y++) {
                    System.out.print(exponent == 1 ? "" : (base + (y < exponent ? "*" : "")));
                    result *= base;
                }
                System.out.print("=" + result);
            }
        }
    }
}

A sample run:

1
2
5
2^5=2*2*2*2*2=32

[Update]

As per your comment, you haven't learnt ternary operator but I strongly recommend you learn it. The solution given below is without using the ternary operator:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int option = keyboard.nextInt();
        int base = keyboard.nextInt();
        int exponent = keyboard.nextInt();
        int result = 1;
        if (option == 1) {
            for (int x = base; x <= base; x++) {
                if (exponent <= 1) {
                    System.out.print(base + "^" + exponent);
                } else {
                    System.out.print(base + "^" + exponent + "=");
                }
                for (int y = 1; y <= exponent; y++) {
                    if (y < exponent) {
                        System.out.print(base + "*");
                    } else if (exponent != 1) {
                        System.out.print(base);
                    }
                    result *= base;
                }
                System.out.print("=" + result);
            }
        }
    }
}

Upvotes: 1

Related Questions