알고리즘 기초 - 32 ( 누적합, 함수 )
Algorithm/풀었던문제 2019. 12. 30. 14:38Main.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 41 42 43 44 45 46 47 48 49 50 51 52 | package Problem32; import java.util.Scanner; public class Main { //두 수를 받아서 두 수 사이에 값을 더해서 반환할 함수 public static int function(int num_1, int num_2) { //total이란 변수를 만들고 반복문이 회전할 때마다 숫자를 계속 더한 후에 total값을 리턴해준다. int total = 0; for(int i = num_1; i <= num_2; i++) { total += i; } return total; } public static void main(String[] args) { //두 수를 입력해서 두 수 사이의 합을 구하기 System.out.println("두 수를 입력하세요."); int num_1; int num_2; Scanner scan = new Scanner(System.in); num_1 = scan.nextInt(); num_2 = scan.nextInt(); int total = function(num_1,num_2); System.out.println("정답 :"+total); } } | cs |
출력결과
출처
https://www.youtube.com/watch?v=hmc_m_eTek4&list=PLVoihNyHW4xkm_KJ8_N8X7F6EQP4uSRyR&index=33
'Algorithm > 풀었던문제' 카테고리의 다른 글
알고리즘 기초 - 31 ( 팩토리얼, 함수 ) (0) | 2019.12.30 |
---|---|
알고리즘 기초 - 30 ( 더하기 & 마이너스 ) (0) | 2019.12.30 |
알고리즘 기초 - 29 ( 음계 ) (0) | 2019.12.30 |
알고리즘 기초 - 28 ( 진수 변환 ) (2) | 2019.12.30 |
알고리즘 기초 - 27 ( 달팽이 출력 ) (0) | 2019.12.29 |