Math with Loops

Optional Node
Collapse Content

To 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

  • It would have been easy if you would have mentioned whether the given problem is to be solved using 'for' or 'while' loop!

  • It 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).

  • This 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;
    }

  • I 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;

  • James, 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.

  • (complete noob running through ) I cant figure this out :(. Can someone please explain what is happening with the Zonko problem...

  • @moses, there are now 3 challenges which all follow the same idea...

  • Anyone having problems, try thinking about it as that a needs to be multiplied by a, b amount of times.

  • This 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;

  • The best thing to do is to take physical notes of the previous lesson and come back to here. My notes really helped.

  • Thanks 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

    cont...
  • This is the easiest way

    <<int Solution = (int) Math.pow(a, b);
    return Solution;

  • int solution = 1;
    for(int i=1; i <= b; i =i+1){
        solution = a * solution;    
    }
    return solution;
    
  • I'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.

  • @learneroo can I get some help in reference to my comment. Thank you.

  • a+b>i does not work as the condition since it doesn't look at i. 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 to a to reach a+b?

All Node Comments
Contact Us
Sign in or email us at [email protected]