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.

the question checks the impllementation of loops. we have to run through a,b and n to get the desired output. Query specifies how many times we have to print the sequences.

You can break the problem by first getting one sequence, and then running it q times over.

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 keep on trying, the answer will come up, my algorithm was something like this
create a "cumulative" variable and set it equal to user input "a"
Inside the INPUT FOR Loop
1)Create a new for loop for a variable say "j", start it from 0 to n where n is the input provided by user and already written in the code.
2)compute the part of (2^j)*b and store it in a variable say "res".
3) Now simply add the equation to the cumulative variable -> cumulative=cumulative+res(refer step 2)
4) Print (cumulative+" ")
5) Close for loop
6) user proper Println syntax tomove cursor to next line.

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.

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.

int tempAnswer = A;
for (int j = 0; j < loop; j++) {
if (j == 0)
tempAnswer += a;
tempAnswer += (int) (Math.pow(2.0, j) * B);
System.out.print(tempAnswer + " ");
}
This will work as we need to add a in the first instance only

This solution is what I considered at first too, but if you do this, you will end with a trailing space. It's better to print hte first number (a + 1 * b) and then print " " + newAnswers in your loop.

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

public static void doJob(int a, int b, int n){
int result=0;
result = a+result;
for (int i = 0 ;i<n;i++){
result =result + (int)Math.pow(2, i)*b;
System.out.print(result + " ");
}
System.out.println();
}

to find a power of value of the result variable using

Math.pow()

which returns a double value as a library method. You thus need to cast it to an int in order for it to work/compile. You will get a casting error otherwise.

Good solution.But inorder to run another test case make sure to add result = 0; after System.out.println(); otherwise it will take previous result value for another test case.

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

increment the value on the right by the value on the left( so i += j means increment j by i). :)
This website seems pretty snobby condsidering the questions asked, how they are explained, and how people talk to each other in the discussions. I did a code camp and know some fairly tricky things and most of these questions seem designed for those who either have math degrees or those who already know a lot about Java and want to keep their skills sharp. "Easy"? Sure... How would a Java beginner even know there is a Math.pow method to import?

Anyway, check out codingbat.com for more straightforward questions that actually range from beginners to advanced. Also look into codecademy.com. Really great resources for FREE.

I solved this.... I believe the "EASY" level classification is incorrect, Because this question requires significant extra analysis And an understanding of mathematical series to approach.

The question in the way that it's written will not be accessible to most beginning and intermediate programmers, unless you have an academic background in Applied math, discrete algebra, or Calculus.

Anyways, they're looking to provide you A list of "a", "b", "n" values, and repeat this process q times.

Each time you get a list of a, b, n values, they want you to print out the integer Sum which you get when you add up 'n series terms' from the expression.

The first series term is

a + 2^0 * b

The second series term is
a + 2^0 * b + 2^1 * b

This process repeats up to "n", and the n-th series term is

a + 2^0 * b + 2^1 * b + .... + 2^(n-1) * b

Your loop just needs to show the cumulative sum on each series term

I suggest working it out on paper with the sample inputs before writing the program.

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

Me neither.. not a scooby doo on what I was been asked. Some of the contributions below shone a light on what was been asked and made it possible to solve.....

dear look at the qustion carefully.....when u focus on it u get more issues and problems but behind that once a time will came ur one confiusion converts to an answer.this remarks the sucess........take difference of its answer u will get solution

You need to take the value of a, b and n and create a series of ans using this (a + 2^0 * b),..., (a + 2^0 * b + 2^n-1 * b)

q is just tells the number of quires you'll be performing,
if q is 3 then you'll have to create 3 sereies of ans using the series that they have given you.

q = 3;

a = 0 b = 2 n = 10

a = 5 b = 3 n = 5

a = 1 b = 8 n = 3

[2, 6,..., 2046]

[8, 14,...,98]

[9, 25, 57]

n will tell you how long will series of ans will be.
a = 0; b = 2 ; n = 10;
(0 + 2^0 * b), [(0 + 2^0 * b) + (2^1 * b)] ... (0 + 2^0 * b + 2^1 * b + a + 2^0 * b )
Your series of ans be [2 , 6 ... ans]

The catch in the series is you only need to add the new 2^n-1 to the previous calculation you did.

This program is working fine, but constant value =0 you defined already right?
If count !=0 then, constant will become 0*2 =0 and it will continue right? can u please explain how it is working.

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();
int seq = a + (int)Math.pow(2, 0) * b;
System.out.print(seq + " ");
for (int j = 1; j < n; j++){
seq = seq + (int)Math.pow(2, j) * b;
System.out.print(seq + " ");
}
System.out.println();
}
in.close();
}

## Java Loops II

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

I am not getting the question., Can anyone please explain what I need to do?

you are not alone... :P the question is a riddle itself.

it's a simple ques. you just need to print the series. look at series.. scan num=a and add 2^n.b and print.. as simple.

i didn't get the why query (q) is used for can you please explain

the question checks the impllementation of loops. we have to run through a,b and n to get the desired output. Query specifies how many times we have to print the sequences.

You can break the problem by first getting one sequence, and then running it q times over.

it show that how many lines of geometric series will come (a+b(2^n-1))

it is just the no. of test cases.

Everything is fine.. but what is q here.??

i think q is used for number of series you want to print . Sample Input

here q=2 . which means 2 series. in 1st series a=0 b=2 n=10 and in 2nd a=5 b=3 c=5. i hope i m correct. (Newbie here)

yes you got it right

On the first series you have n and the second you have c? explain please

I think that is a typing error, in the second series the values are a=5, b=3 and n=5.

Hope this helps!!!!

in the given test case q=2 an in next q=3 i.e. a+q^0b+q^1b.....

It's super confusing, but "q" is taken care of for you and is actually called "t". Wtf! Yeah, I know.

but sir this solution is not satisfying all the test cases.

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.

u are correct bro..but i amnot getting the array declaration in java..can you help me?

In the U.S, when somebody says something is hard, you're not suppose to say, "its so simple look". xD

it's a simple ques. you just need to print the series. look at series.. scan num=a and add 2^n.b and print.. as simple.

how i can increment the number 'n' value for getting the series, i dont know how to put loop for this, can anyone plz help me

Hey,

Just keep on trying, the answer will come up, my algorithm was something like this create a "cumulative" variable and set it equal to user input "a" Inside the INPUT FOR Loop 1)Create a new for loop for a variable say "j", start it from 0 to n where n is the input provided by user and already written in the code. 2)compute the part of (2^j)*b and store it in a variable say "res". 3) Now simply add the equation to the cumulative variable -> cumulative=cumulative+res(refer step 2) 4) Print (cumulative+" ") 5) Close for loop 6) user proper Println syntax tomove cursor to next line.

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.

same with me

but it doesn't pass through all the test cases.

Riddle is solved, Check the solution above

You have to look at the equation at the top. That is the trickiest part.

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 .

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

int A = 5; int B = 3; int loop = 5;

It will not work. First line i.e. int tempAnswer = A; It will fail.

yes, u are right,, but what is the reason behind it??

It should be an "a".

int tempAnswer = 0; for (int j = 0; j < loop; j++) { tempAnswer += (Math.pow(2, j) * b); System.out.print(tempAnswer + a + " "); }

nice

when i write tempAnswer = tempAnswer + (Math.pow(2,j)*b); it gives an error of loss of precision. Can you please explain me why is it so?

pow() gives a double value,but you are using an integer so the precision is lost..Type cast the pow() function to integer . You will get it done.

In case you haven't noticed, first you need to take all the inputs and then output according to each of them. Not, give the output after every, input.

what is the need of taking two seperate variables: storage and formula

If you use "println" then every number will be printed in next line..........

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

VIT VIT VIT

Lol

*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

it will work and it works properly dude.

YOur Code is aWesOmE.

Your code is helpful!

take in small case alphabets i.e. "a"

This solution is what I considered at first too, but if you do this, you will end with a trailing space. It's better to print hte first number (a + 1 * b) and then print " " + newAnswers in your loop.

This one works as it should I guess:

import java.util.

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

}

why do we need int before Math.pow

You are using

to find a power of value of the result variable using

which returns a double value as a library method. You thus need to cast it to an int in order for it to work/compile. You will get a casting error otherwise.

pow() takes both of its arguments as double.

pow() results in double

That implies type casting

type casting

that int is needed for type casting or for converting double data type to int type , as we know that Math funcction is of double..

Good solution.But inorder to run another test case make sure to add result = 0; after System.out.println(); otherwise it will take previous result value for another test case.

why are you creating another method? Why not do the same thing in main method?

it fails not work

According to me best solution

*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

You can use the .trim() method on your output string to remove the trailing space:

int runningTotal = a; String outputString = ""; int multiplier = 1;

for(int count = 1; count <= n; count++) { runningTotal += multiplier * b; outputString += runningTotal + " "; multiplier *= 2; }

System.out.println(outputString.trim());

It solves the problem of a^(count-1). Thank you.

import java.util.

; import java.io.; import java.lang.*;class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int t=in.nextInt(); int x=0; int y[]=new int[15]; for(int i=0;i

}

why have you subtituted (int) in the line no 3?

for converting double to int

converting from double to int becoz pow function gives the double value got it ??

maybe in the first line int tempAnswer = 0;

Thanks vinityadav !! it works! private static String answer(int a, int b, int n){ String answer = ""; int tempAnswer= a; for (int i = 0; i < n; i++) { tempAnswer += (int) (Math.pow(2.0, i) * b); respuesta = respuesta+ " " + tempAnswer; }

what is +=??

increment the value on the right by the value on the left( so i += j means increment j by i). :) This website seems pretty snobby condsidering the questions asked, how they are explained, and how people talk to each other in the discussions. I did a code camp and know some fairly tricky things and most of these questions seem designed for those who either have math degrees or those who already know a lot about Java and want to keep their skills sharp. "Easy"? Sure... How would a Java beginner even know there is a Math.pow method to import?

Anyway, check out codingbat.com for more straightforward questions that actually range from beginners to advanced. Also look into codecademy.com. Really great resources for FREE.

dang this helps a lot. Thank you so much

you made it more complex

import java.util.

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

}

dont use Math.pow function.

tq

Thank you :)

It's wrong bro

I solved this.... I believe the "EASY" level classification is incorrect, Because this question requires significant extra analysis And an understanding of mathematical series to approach.

The question in the way that it's written will not be accessible to most beginning and intermediate programmers, unless you have an academic background in Applied math, discrete algebra, or Calculus.

Anyways, they're looking to provide you A list of "a", "b", "n" values, and repeat this process q times.

Each time you get a list of a, b, n values, they want you to print out the integer Sum which you get when you add up 'n series terms' from the expression.

The first series term is

a + 2^0 * b

The second series term is a + 2^0 * b + 2^1 * b

This process repeats up to "n", and the n-th series term is

a + 2^0 * b + 2^1 * b + .... + 2^(n-1) * b

Your loop just needs to show the cumulative sum on each series term

I suggest working it out on paper with the sample inputs before writing the program.

its easy peasy

Easy for someone with a degree in mathematics. Algorithms aren't "easy".

import java.util.

; import java.io.; import java.math.*;class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int t=in.nextInt(); int s=0; int v=0; for(int i=0;i System.out.print(v+" ");

}

this code works fine

Me neither.. not a scooby doo on what I was been asked. Some of the contributions below shone a light on what was been asked and made it possible to solve.....

import java.util.

; import java.io.; import static java.lang.Math.pow; class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int t=in.nextInt(); for(int i=0;i } in.close(); } }dear look at the qustion carefully.....when u focus on it u get more issues and problems but behind that once a time will came ur one confiusion converts to an answer.this remarks the sucess........take difference of its answer u will get solution

this is what i got:

for(int i=0;i

NOO

You need to take the value of a, b and n and create a series of ans using this (a + 2^0 * b),..., (a + 2^0 * b + 2^n-1 * b)

q is just tells the number of quires you'll be performing, if q is 3 then you'll have to create 3 sereies of ans using the series that they have given you.

q = 3;

a = 0 b = 2 n = 10

a = 5 b = 3 n = 5

a = 1 b = 8 n = 3

[2, 6,..., 2046]

[8, 14,...,98]

[9, 25, 57]

n will tell you how long will series of ans will be. a = 0; b = 2 ; n = 10; (0 + 2^0 * b), [(0 + 2^0 * b) + (2^1 * b)] ... (0 + 2^0 * b + 2^1 * b + a + 2^0 * b ) Your series of ans be [2 , 6 ... ans]

The catch in the series is you only need to add the new 2^n-1 to the previous calculation you did.

This program is working fine, but constant value =0 you defined already right? If count !=0 then, constant will become 0*2 =0 and it will continue right? can u please explain how it is working.

the value of constanst is 1 after first execution

for(int i=0;i

same here :D

Here is full code: but please first understand it before use...

import java.util.

; import java.io.;class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int t=in.nextInt(); int temp = 0; for(int i=0;i for(int j = 1; j <= n; j++){ temp += (int)(Math.pow(2, j-1) * b); System.out.print((a + temp) + " "); } temp = 0; System.out.printf("\n"); } } }

System.out.print((a + temp) + " ");

was really helpful

int replace = 0; for (int j = 0; j < n; j++) { int math = (int) Math.pow(2, j); int sum0 = (math * b); int ver = sum0; replace += ver; int asd = a + replace; System.out.printf("%d ", asd); } System.out.println(""); } in.close();

Solution

for each of the 'q' queries you are given three values - (a,b,n)

( a + (b * (2^i)) )Yah, It's a simple question to loop and print a series of the form (a+1*b)+(a+1*b+2*b)+(a+1*b+2*b+4*b)+...........

You need to append always Math.pow(2,j) times b i.e 2^1*b,2^2*b....

Hope it helps.

import java.util.

; import java.math.; class sos { public static void main(String args[]) { Scanner sc=new Scanner (System.in); int i,n,a,j,b,k=0,l,s1; l=sc.nextInt(); for(int m=0;m=1&&n<=15) { for(i=0;i=0) s=s+a; k++; //if(k%10!=0) s1=(int)s; System.out.print(s1+" "); //else //{ //System.out.println();}