Write a method header on line two with the following specs:
Returns: an integer Name: sumFivesRange Parameters: an integer called "a" that represents the beginning of the range an integer called "b" that represents the end of the range Purpose: calculate the sum of the multiples of 5 within the range a to b inclusive (including b) |
Examples:
- sumFivesRange(5,15) ==> 30 sumFivesRange(11,28) ==> 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Main {
public static int sumFivesRange(int a,int b)
{
int sum=0;
for(int i=a;i<=b;i++) {
int temp=i%5==0?i:0;
sum+=temp;
}
return sum;
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(sumFivesRange(5,15)); //30
System.out.println(sumFivesRange(11,28)); //60
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
049 - Accumulator Method Practice 5 (0) | 2019.12.26 |
---|---|
048 - Accumulator Method Practice 4 (0) | 2019.12.26 |
045 - Accumulator Method Practice 1 (0) | 2019.12.19 |
044 - Method Header Challenge 1 (Optional) (0) | 2019.12.19 |
043 - Method Header Practice 8 (0) | 2019.12.19 |