본문 바로가기
Baekjoon Online Judge

[BOJ] 백준 10869번 사칙연산 (Java)

by 프로그래 밍구 2022. 11. 7.

문제링크

https://www.acmicpc.net/problem/10869

 

10869번: 사칙연산

두 자연수 A와 B가 주어진다. 이때, A+B, A-B, A*B, A/B(몫), A%B(나머지)를 출력하는 프로그램을 작성하시오. 

www.acmicpc.net


나의코드

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a, b;
        a = sc.nextInt();
        b = sc.nextInt();
        sc.close();
        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b);
        System.out.println(a / b);
        System.out.print(a % b);
    }
}

 

Scanner 클래스로 입력받은 정수 a 와 b 를 사칙연산하여 출력하였다. 나머지를 구하는 방법은 %를 사용하는 것이다. 출력할 때 줄바꿈을 하기 위해 System.out.println( ); 메소드를 사용하였다.

댓글