java - How do I get the program to display the sum of the values divisible by 5, versus just the values themselves? -
the program asks method created. method takes 2 parameters: start , end, both integers. method must sum of numbers between start , end divisible 5. example, if start 1 , end 30, answer must 105, since 5 + 10 + 15 + 20 + 25 + 30 = 105 numbers divisible 5 , belong range of 1 , 5.
import java.util.*; public class divisor{ public static void main(string[] args){ scanner input = new scanner(system.in); system.out.print("enter start: "); int start = input.nextint(); system.out.print("enter end: "); int end = input.nextint(); int result6 = sumdivisor(start, end); system.out.println(result6); } public static int sumdivisor (int start, int end){ int value = end; for(int = 5;i <= end;i = + 5){ value = i; system.out.print(i + " "); } return value; } }
you have take account situation start argument not divisible 5:
public static int sumdivisor (int start, int end){ int value = 0; while (start % 5 != 0) { start++; } for(int = start;i <= end;i += 5){ value += i; } return value;
}
Comments
Post a Comment