jubins
jubins

Reputation: 1

How do I add power in java?

I am new to Java and stackoverflow. I am writing a program that can add power in Java, for example: 2^1, 2^1+2^2, 2^1+2^2+2^3,.. so on.

I have written below program and I don't know what I am doing wrong when I am trying to add the powers. I just get 2^1 2^2 2^3,... as output. I hope you get the idea from my code and it will be a great help if you guys can help me learn.

Thank you in advance!

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a: ");
    int a = sc.nextInt(); //a = first number
    System.out.print("Enter b: ");
    int b = sc.nextInt(); //b = second number
    System.out.print("Enter t: ");
    int t = sc.nextInt(); //t = no. of iterations

    int x=0, sum = 0;

    for (int i = 0; i < t;) {

        for (int j = 0; j < t; j++) {
            int pow = (int) Math.pow(2, i);
            x = a + (pow * b);          

            i++;

            System.out.printf("%d ", x);
            sum = x;
        }

        sum = x + sum;

        System.out.println(sum);
    }
}

Upvotes: 0

Views: 350

Answers (3)

Bahramdun Adil
Bahramdun Adil

Reputation: 6089

Very simply you can do it as below:

public static int getPow(int num, int pow) {
    if (pow < 2) {
        return num;
    }
    return (int) Math.pow(num, pow) + getPow(num, --pow);
}

Usage:

int pow = getPow(2, 4);// 2*1 + 2*2 + 2*2*2 + 2*2*2*2 = 2+4+8+16 = 30
System.out.println("pow = " + pow);

And Result:

pow = 30

Upvotes: 0

CodeRunner
CodeRunner

Reputation: 687

According to Mathematics rules, if it is addition among the numbers, for example 2^1 + 2^2 + 2^3 + 2^4... Then it is simple you don't need two loops and the t variable. You just need the base and the last exponent limit.

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the base: ");
        int a = sc.nextInt(); //a = first number
        System.out.print("Enter iterations: ");
        int b = sc.nextInt(); //b = No of iterations
        int sum = 0;
        for (int i = 1; i <= b; i++) {
            sum += Math.pow(a, i);
        }
        System.out.println("The sum is " + sum);
    }

But if there is multiplication among the numbers, then you will add the exponents if the base is same. Fox example 2^1 * 2^2 * 2^3 * 2^4.... Then you may do it as below.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the base: ");
    int a = sc.nextInt(); //a = first number
    System.out.print("Enter iterations: ");
    int b = sc.nextInt(); //b = No of iterations
    Double res;
    int powerSum = 0;
    for (int i = 1; i <= b; i++) {
        powerSum += i;
    }
    System.out.println("Power sum is " + powerSum);
    res = Math.pow(a, powerSum);
    System.out.println("The result is " + res);
}

Upvotes: 1

Moshe Stauber
Moshe Stauber

Reputation: 401

In your inner loop

int pow = (int) Math.pow(2, i);

shouldn't you be using j instead of i?

Upvotes: 0

Related Questions