(Coderbyte) Simple Adding - Solução

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.

Solução:

import java.util.*;
import java.io.*;

class Function { 
  int SimpleAdding(int num) {
    int sum = 0;
    for (int i = 1; i <= num; i++) {
        sum += i;
    }
   
    return sum;
   
  }
 
  public static void main (String[] args) { 
    // keep this function call here    
    Scanner  s = new Scanner(System.in);
    Function c = new Function();
    System.out.print(c.SimpleAdding(s.nextLine()));
  }  
 
}          

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução