[Rust로 백준 하루 하나] 3-3. 합

김진산·2024년 8월 3일

Rust로 백준 하루 하나

목록 보기
23/138
post-thumbnail

문제 (8393번)

n이 주어졌을 때, 1부터 n까지 합을 구하는 프로그램을 작성하시오.

입력

첫째 줄에 n (1 ≤ n ≤ 10,000)이 주어진다.

출력

1부터 n까지 합을 출력한다.


풀이

코드

use std::io;

fn main() {
    let mut input = String::new();
    
    io::stdin()
        .read_line(&mut input)
        .expect("Cannot read line.");
    
    let input: u32 = input.trim().parse().unwrap();
    let mut sum = 0;
    
    for i in 1..=input {
        sum += i;
    }
    println!("{}", sum);
}

해설

특이사항 없음


추가 학습

  • 특이사항 없음
profile
블록체인 개발자

0개의 댓글