Thursday, 3 December 2015

Kata Day 9: Sum of numbers


Problem Description: Given two integers, which can be positive and negative, find the sum of all the numbers between including them too and return it. If both numbers are equal return a or b.

Note! a and b are not ordered!

Level: 7kyu

Link to Kata: Sum of numbers

A pretty straightforward Kata. We have to find the sum of numbers between two numbers including the numbers themselves. 

We will be initializing a sum variable to zero in the starting. Then we compare the given two numbers. If both numbers are equal we output the same number without any further operation. Otherwise we start to move from the smaller number towards the bigger number and keep adding the number on every looping index to the sum variable. Once we have reached end of the loop we will be returning the sum as our output.

Here is a link to the code: Kata Day 9

Please mention your suggestions or your doubts in the comment below.

Happy coding. :) 

No comments:

Post a Comment