We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Math.pow() is a function that is provided to us by Java. Here, it takes 2 parameters. When I pass in 2 and j, it does exponentiation (known as "power") as 2 to the j-th power. For example, Math.pow(3,5) = 3 * 3 * 3 * 3 * 3.

Also, Math.pow() returns a double instead of an int. We "cast" it to an integer by putting

(int)

in front of it. We cast our result since a is an integer type and we can only assign an int type to it.

class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for (int j = 0; j < n; j++) {
int c = a + b * (int) Math.pow(2, j);
System.out.print(a + " ");
a = c;
}
System.out.println();
}
in.close();
}
}

## Java Loops II

You are viewing a single comment's thread. Return to all comments →

Can you please explain what does this line do? a += b * (int) Math.pow(2, j);

is shorthand for

Math.pow() is a function that is provided to us by Java. Here, it takes 2 parameters. When I pass in

2andj, it does exponentiation (known as "power") as 2 to the j-th power. For example, Math.pow(3,5) = 3 * 3 * 3 * 3 * 3.Also, Math.pow() returns a

doubleinstead of anint. We "cast" it to an integer by puttingin front of it. We cast our result since

ais an integer type and we can only assign aninttype to it.Hope this helps.

HackerRank solutions.

Thank you very much for explaining me.

what's wrong with this? can you please tell me

I think the values for "a" and "c" are incorrect inside the inner for loop.

where the value of t is used ?what is neccesary of it in this code....

t is number of test cases

Great explanation. Thank you so much.

thank u mam

The value of 'b x 2^j' gets added to 'a' in that expression. It is like 'a = a + (b x 2^j)'. Hope this helps.