(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()));
}
}
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
Post a Comment