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.

In every Iteration you should add previous result so that you would get result
....................................
1st Iteration:
result=a+2^0*b
.........................
2nd Iteration:
result = result + (2^1)*b
.........................
3rd Iteration:
result =result + (2^2)*b
........................
N^th Iteration:
result = result+ (2^n-1)*b
...........I hope It will help you!!!!!!!

i mean first you have to calculate the first term then see that the first term is common in all terms so you have to just appy a loop after that , and in that loop i have told that what we have to perform. Now just simply iterate that loop and multilpy that first term to the matter that you calculated inside the loop.

but in that case we would get a space after the last element.
but accroding to test case (given) we should not get a space after last element.
so how can you modify without getting a space at end the end in your code

## Java Loops II

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

a+2^0*b+2^1*b+2^2*b+2^3*b+........+2^n-1*b

In every Iteration you should add previous result so that you would get result .................................... 1st Iteration: result=a+2^0*b ......................... 2nd Iteration: result = result + (2^1)*b ......................... 3rd Iteration: result =result + (2^2)*b ........................ N^th Iteration: result = result+ (2^n-1)*b ...........I hope It will help you!!!!!!!

Thank you. Solved the problem now!

Will you tell me the Code how it solved.

for (int j = 0; j < n; j++) { a += b; System.out.print(a + " "); b *= 2; } System.out.println();

Thanks for sharing, found it very simple to understand.

Not sure why it's been down voted. It's a brilliant solution. Helped me a lot to understand what's going on. Thanks mate

Out of all the complicated solutions, you gave the simplest solution ever. Brilliant thinking! Thanks for sharing.

I am glad my comment helped you ! Keep Coding :D

really you make it very simple...too good

i didnt understand your logic can u explain it please

i mean first you have to calculate the first term then see that the first term is common in all terms so you have to just appy a loop after that , and in that loop i have told that what we have to perform. Now just simply iterate that loop and multilpy that first term to the matter that you calculated inside the loop.

Just brilliant!!!

LOL! I've made a "local" class similar to pow... XD (And completed the test) But... omg your answer were soo simple and efficient...

This is the best. I was using Math.pow unnecessarily.

good logic but it's is not working giving some runtime exception .

i just want to know that how does your power increments for 2 in this code

for(int i=0;i

look at this

but in that case we would get a space after the last element. but accroding to test case (given) we should not get a space after last element. so how can you modify without getting a space at end the end in your code

you can run and check, if in test case space is not allowed after last element you can add space till n-1 length and skip for other.

thanks for helping us.

But by this logic it will add 'a' each loop we only have to add 'a ' first iteration only....

then y did i get wrong output here?

class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int t=in.nextInt(); int s=0; int j; for(int i=0;i

superb explanation