Using recursion to sum numbers

后端 未结 16 2068
轮回少年
轮回少年 2021-02-05 15:45

I have just been studying the concept of recursion and I thought that I would try a simple example. In the following code, I am attempting to take the numbers: 1, 2, 3, 4, 5, an

16条回答
  •  鱼传尺愫
    2021-02-05 16:00

    Actually, I think you don't need to check case else because

    public static int sum(int number){
        if(number > 0){
           return number + sum(--number);
        }
        return number; // return 0 so that's why you don't need check else condition
    }
    

提交回复
热议问题