본문 바로가기

프로그래밍 문제/정올

135 : 반복제어문2 - 형성평가6

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        int sum = 0, count = 0;
        double avg = 0;
 
        if (num1 < num2){
            for (int i=num1; i<=num2; i++){
                if ((i%3== 0) {
                    sum += i;
                    count++;
                }
                else if ((i%5== 0) {
                    sum += i;
                    count++;
                }
            }
        }
        else {
            for (int i=num2; i<=num1; i++){
                if ((i%3== 0) {
                    sum += i;
                    count++;
                }
                else if ((i%5== 0) {
                    sum += i;
                    count++;
                }
            }
        }
        avg = (double)sum/count;
 
        System.out.printf("sum : %d\n", sum);
        System.out.printf("avg : %.1f", avg);
    }
}
cs