Math with Loops
Optional NodeTo get some practice with loops, see if you can finish the code in the challenges below. Then solve a similar coding challenge on the bottom.
The Addition Ban
Zonko's government (The Kingdom of Zumbania) decided to ban directly adding numbers greater than 1 to other numbers. Zonko decided to create his own addition method (for positive integers) to circumvent the ban. Can you help finish his code?
To get the sum of a
and b
, Zonko starts by setting a variable sum
equal to a
. He then repeatedly adds 1
to sum
until it reaches a+b
. 1
is added every iteration of the loop until its been added the right number of times. What is the correct condition in the loop so it stops at the right time?
public int add(int a, int b){
int sum = a;
for(int i=1; LOOP-CONDITION; i=i+1){
sum = sum + 1; //this will add 1 to sum every iteration
}
return sum;
}
Multiplication Ban
The Kingdom of Zumbania decided to allow addition and ban the multiplication character *
instead. Luckily, Zonko figured out how to write a multiplication method (for positive integers) by using addition. Can you help finish the code?
The method starts with a variable product
set to 0. It then adds x
to the product in each iteration of the loop. How many times should the loop run for product
to be equal to the actual product of x
and y
?
public int multiply(int x, int y)
{
int product =0 ;
for(int i=1; LOOP-CONDITION ; i =i+1){
product = x + product; //this will increase product by x every iteration
}
return product;
}
Challenge
Help Zonko out by writing the code to replace LOOP-CONDITION
above with a condition that will stop when the correct product has been reached.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Challenge
Given two positive integers a and b, return ab. (I.e. Calculate the exponential function for a
to the b
.)
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Challenge
Help Zonko out by writing the code to replace LOOP-CONDITION above with a condition that will stop when the correct sum has been reached.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Jay Learn
Jul 23, 10:28 AMIt would have been easy if you would have mentioned whether the given problem is to be solved using 'for' or 'while' loop!
Learneroo
Jul 25, 5:26 PMIt can be solved with either kind of loop. This question may be too hard, so I will add some more explanation (and a cheat option).
James Montgomery
Aug 29, 1:00 AMThis is what I did, I don't understand the setting of variable to 1, I set mine to 2 and it worked.
public static int doStuff(int a, int b){
int product=a;
for(int i=2;i<=b;i=i+1){product=product*a;}
return product;
}
James Montgomery
Aug 29, 1:10 AMI changed it, and it still works, but i think this is more correct and will have less errors with certain numbers. public static int doStuff(int a, int b){
int product=1;
for(int i=1;i<=b;i=i+1){product=product*a;}
return product;
Learneroo
Sep 1, 6:48 PMJames, I changed the explanation to just show the code. My mention of setting the variable to 1 would have been relevant if I had cases where the exponent was 0, but I decided to leave them out. This means your first solution would also work.
moses
Nov 11, 12:24 PM(complete noob running through ) I cant figure this out :(. Can someone please explain what is happening with the Zonko problem...
Learneroo
Nov 12, 7:25 PM@moses, there are now 3 challenges which all follow the same idea...
Tyler Pond
Nov 22, 12:29 PMAnyone having problems, try thinking about it as that a needs to be multiplied by a, b amount of times.
Victoria Holland
Jan 14, 5:11 AMThis was a tough one. I originally put the following code, which was incorrect:
int solution = 0;
for (int i=0; i<=b; i++)
{
solution += (a * a);
}
return solution;
Jamison Norwood
Feb 7, 12:26 PMThe best thing to do is to take physical notes of the previous lesson and come back to here. My notes really helped.
David
Feb 21, 7:22 PMThanks much from learneroo for helping noobs like myself to learn a bit more. My 25 cents for this exercise even though i may be going to ahead of time is that it would have been useful to know
that the return must go out of the for loop and the reason.. Or was this explanation on an old lesson o_O? Anyway cheers yall!
Oliver Cross
Apr 18, 9:31 PMThis is the easiest way
<<int Solution = (int) Math.pow(a, b);
return Solution;
Colt Wolff
May 20, 11:47 AMBernard Mitchell
Jun 21, 10:28 PMI'm stuck on the first one. The loop stops when the condition is no longer true. If I do a+b>i; . Sum=sum+1 aka a=a+1; will run until the condition is no longer true. However I keep getting incorrect. Appreciate the advice thanks.
Bernard Mitchell
Jun 23, 10:20 AM@learneroo can I get some help in reference to my comment. Thank you.
Learneroo
Jun 23, 10:31 AMa+b>i
does not work as the condition since it doesn't look ati
. Try it out with an example, e.g. where a is 3 and b is 5. Also, the goal is to not add numbers greater than 1 together. How many times should you add 1 toa
to reach a+b?