1. 입력된 배열의 합이 100을 넘어갈때의 합을 구하라.

2. 1~10까지의 수중 양수의 합과 음수의 합을 구하라.

3. 5개의 숫자를 입력받아 총점과 평균을 구하라.

 

 

1. CLASS

 

package test002;

 

import java.io.IOException;
import java.util.Scanner;

 

public class cal_00 {
  public static void main(String args[]) {

    int[] a = {30,40,50,60};
    int hap = 0;
    String hap1 = "자연수 : ";

 

    for(int i = 0;i<a.length;i++){
      hap += a[i];


      if(hap > 100){
        hap1 += a[i] + " = ";
        System.out.println("1. 입력된 배열의 합이 100을 넘어갈때의 합을 구하라.");
        System.out.println(hap1 + hap + "\n");
        break;
      }else{
        hap1 += a[i] + " + ";
      }
    }
  
    int hapP = 0;
    int hapM = 0;
  
    for(int j = 1;j<10;j++){
      int gubun = j%2;

      if(gubun == 1){
        hapM += j;
      }else{
        hapP += j;
      }
  }
  System.out.println("2. 1~10까지의 수중 양수의 합과 음수의 합을 구하라.");
  System.out.println("양수의 합 : " + hapP + " / 음수의 합 : " + hapM + "\n");
  
  Scanner input = new Scanner(System.in);
  int tot = 0;
  int avg = 0;


  System.out.println("3. 5개의 숫자를 입력받아 총점과 평균을 구하라. ");

    for(int i=0;i<5;i++){
      tot += input.nextInt();
    }
    avg = tot/5;
    System.out.println("총점 : " + tot + " / 평균 : " + avg );
  }
}

 

 

2. 실행결과

 

1. 입력된 배열의 합이 100을 넘어갈때의 합을 구하라.
자연수 : 30 + 40 + 50 = 120

2. 1~10까지의 수중 양수의 합과 음수의 합을 구하라.
양수의 합 : 20 / 음수의 합 : 25

3. 5개의 숫자를 입력받아 총점과 평균을 구하라.
20
30
80
90
98
총점 : 318 / 평균 : 63