Simple Adding Coderbyte Solution

0

Have the function SimpleAdding(num) add up all the numbers from 1 to num. For the test cases, the parameter num will be any number from 1 to 1000.



Simple Adding Coderbyte Solution In Java

import java.util.Scanner;


public class SimpleAdding {
int SimpleAdding(int num) {
int sum = 0;
for (int i = 0; i <= num; i++) {
sum += i;
}

return sum;

}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
SimpleAdding c = new SimpleAdding();
System.out.print(c.SimpleAdding(s.nextInt()));
}
}

Simple Adding Coderbyte Solution In Python

def SimpleAdding(num):
sum = 0
for x in range(1,num+1):
sum = sum + x
return sum
print(SimpleAdding(input()))


Simple Adding Coderbyte Solution In JavaScript

Using the JavaScript language, have the function SimpleAdding(num) add up all the numbers from 1 to num. For the test cases, the parameter num will be any number from 1 to 1000.    


function SimpleAdding(num) {

var tot = 0;
for (var i = 1; i <= num; i++) {
tot += i;
}
return tot;
}

This is a simple iterative function that add each number in sequence.

Steps for solution                                                                  

    1) Set var tot to 0.                                                              

   2) Loop from 1 to num and add i by tot to get new tot.                            

    3) Return tot for answer.     

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !