Solutions for Simple Adding
View the challenge

This challenge requires you to add up all the numbers from 1 to a given argument. For example, if the parameter num is 4, your program should add up 1 + 2 + 3 + 4 and return 10. This will be pretty simple to write out as a loop. We'll maintain a variable and keep adding to it as we loop from 1 to num.

function SimpleAdding(num) { 

  var answer = 0;

  // loop from 1 to num
  for (var i = 1; i <= num; i++) {
    answer = answer + i;
  } 

  return answer;
         
}

SimpleAdding(4);                                                    
def SimpleAdding(num): 
  
  answer = 0

  # loop from 1 to num
  for i in range(1, num+1):
    answer = answer + i
    
  return answer
    
print SimpleAdding(4)  
def SimpleAdding(num)

  answer = 0
  
  # loop from 1 to num
  (1..num).each do |i|
    answer += i
  end
  
  return answer
         
end
     
puts SimpleAdding(STDIN.gets)    

There is also a very clever way to calculate the sum of the numbers 1 + 2 + 3 + .... The sum of the first N natural numbers is equal to n(n+1)/2 (see proof and why this is so).

function SimpleAdding(num) { 

  return (num*(num+1))/2;
         
}

SimpleAdding(4);                                                    
def SimpleAdding(num): 
  
  return (num*(num+1))/2
    
print SimpleAdding(4)  
def SimpleAdding(num) 
  
  return (num*(num+1))/2
    
end

puts SimpleAdding(STDIN.gets)  
Top User Solutions
1 hyuil Challenge Score: 10 Cpp
1 cjordanball Challenge Score: 10 JavaScript
2 atib1980 Challenge Score: 10 Cpp
1 kalgima Challenge Score: 10 Swift
1 ssprogis Challenge Score: 10 Python
2 hunsoo Challenge Score: 10 JavaScript
2 Qlogin Challenge Score: 10 Python
3 gutty333 Challenge Score: 10 Cpp
3 leonhfr Challenge Score: 10 JavaScript
3 KlaraKathrina Challenge Score: 10 Python
1 emuchynski Challenge Score: 10 Ruby
4 Sabin Challenge Score: 10 Python
2 junjunparkpark Challenge Score: 10 Ruby
5 Corinto Challenge Score: 10 Python
6 bneeriemer Challenge Score: 10 Python
7 lesian Challenge Score: 10 Python
8 Ryantimjohn Challenge Score: 10 Python
4 EmperorDuny Challenge Score: 10 JavaScript
5 jrorsini Challenge Score: 10 JavaScript
6 leot75 Challenge Score: 10 JavaScript
3 Ryanm1234 Challenge Score: 10 Ruby
7 nulln Challenge Score: 10 JavaScript
9 gzamfire Challenge Score: 10 Python
10 mmalkavian Challenge Score: 10 Python
1 himanshusharma18 Challenge Score: 10 Java
8 mattlarsh Challenge Score: 10 JavaScript
2 VatsalGupta Challenge Score: 10 Java
9 beeboop19 Challenge Score: 10 JavaScript
4 lokeshmogra Challenge Score: 10 Cpp
5 cdms0 Challenge Score: 10 Cpp
4 rnelson082 Challenge Score: 10 Ruby
10 jackgeek Challenge Score: 10 JavaScript
3 ddwivedy Challenge Score: 10 Java
5 memyselfandhai Challenge Score: 10 Ruby
6 kmosier Challenge Score: 10 Ruby
7 lucanioi Challenge Score: 10 Ruby
8 Bohdan Challenge Score: 10 Ruby
6 sanguookchoi Challenge Score: 10 Cpp
9 SSJ6Porfy Challenge Score: 10 Ruby
10 stevenli Challenge Score: 10 Ruby
4 arjnklc Challenge Score: 10 Java
5 dakotalillie Challenge Score: 10 Java
6 debasisdwivedy Challenge Score: 10 Java
7 theBestFella Challenge Score: 10 Cpp
8 terry Challenge Score: 10 Cpp
9 johne Challenge Score: 10 Cpp
1 ionutforyou Challenge Score: 10 PHP
10 Justification Challenge Score: 10 Cpp
7 danaksim Challenge Score: 10 Java
8 jakmaj Challenge Score: 10 Java
9 mikejagen Challenge Score: 10 Java
10 AjeethKannan Challenge Score: 10 Java
2 longcheng Challenge Score: 10 PHP
3 torbjornperttu Challenge Score: 10 PHP
4 albionselimaj Challenge Score: 10 PHP
5 teplicko Challenge Score: 10 PHP
6 Althalus Challenge Score: 10 PHP
7 smcdowell Challenge Score: 10 PHP
8 jetonr Challenge Score: 10 PHP
9 khattab Challenge Score: 10 PHP
10 devilin Challenge Score: 10 PHP
2 ajuljulian Challenge Score: 10 Swift
3 Ess30 Challenge Score: 9 Swift
4 Sokrates Challenge Score: 10 Swift
5 designergenes Challenge Score: 10 Swift

Become a Premium Member

With our large collection of challenges, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

“Daniel - Thanks so much for making Coderbyte! We still recommend it to people.” ― Shawn Drost
  • 200+ Coding Challenges
  • Mock Interview Questions
  • 900,000+ Code Solutions
  • Algorithm Tutorials
  • Interview Prep Courses