Skip to Content
CoursesCSC122การบวกและลบตามเงื่อนไข Modulo 13

โจทย์

Given an integer value a, return the sum between a and the next five integer values greater than a. However, when any of the next five integer value is divisible by 13, subtract the number instead. For example, if a = 10, the method will return 10+11+12 -13 +14+15, which is 49.

InputOutput
1049
-23-123
26171

โค้ด

import java.util.*; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int startInt = scanner.nextInt(); int sum = startInt; for(int i = 1; i <= 5; i++) { int nextNumber = startInt + i; sum += nextNumber % 13 == 0 ? -nextNumber : nextNumber; } System.out.println(sum); } }

คำอธิบาย

ปรับปรุงล่าสุด