[프로그래머스]두 정수 사이의 합

mongs_Develop·2022년 5월 12일
0

Programmers-Level1-Java

목록 보기
26/30
post-thumbnail
  • 문제 & 예시

  • 소스코드

// 두 정수 사이의 합
public class test26 {
	public static void main(String[] args) {
		Solution26 sol = new Solution26();
		int a = 3;
		int b = 5;
		System.out.println(sol.solution(a, b));
	}
}
class Solution26 {
    public long solution(int a, int b) {
        long answer = 0;
        
        // a보다 b가 큰경우
        // b보다 a가 큰경우
        // a와 b가 같은 경우
        if(a<b) {
        	for(int i=a;i<=b;i++) {
            	answer += i;
            }
        }else if(a>b) {
        	for(int i=b;i<=a;i++) {
            	answer += i;
            }
        }else {
        	answer = a;
        }
          
        return answer;
    }
}
  • consol
profile
개 발 인생

0개의 댓글