Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input : 12345 Output : 15 Input : 45632 Output :20
Bàn luận
Let the number be 12345.
Step 1-> 12345 % 10 which is equal-too 5 + [ send 12345/10 to next step ]
Step 2-> 1234 % 10 which is equal-too 4 + [ send 1234/10 to next step ]
Step 3-> 123 % 10 which is equal-too 3 + [ send 123/10 to next step ]
Step 4-> 12 % 10 which is equal-too 2 + [ send 12/10 to next step ]
Step 5-> 1 % 10 which is equal-too 1 + [ send 1/10 to next step ]
Step
6-> 0 algorithm stops
following diagram will illustrate the process of recursion
C++
#include
Đưa ra một số, chúng ta cần tìm tổng số các chữ số của nó bằng cách sử dụng recursion.examples: & nbsp; & nbsp;
Quy trình từng bước để hiểu rõ hơn về cách thức hoạt động của thuật toán. & nbsp; Bước 2-> 1234 % 10, bằng-too 4 + [gửi 1234/10 đến bước tiếp theo] & nbsp; bước 3-> 123 % 10 bằng nhau-too 3 + [gửi 123/10 đến bước tiếp theo] & nbsp; Bước 4-> 12 % 10 bằng-too 2 + [gửi 12/10 đến bước tiếp theo] & nbsp; Bước 5-> 1 % 10 bằng nhau-too 1 + [gửi 1/10 đến bước tiếp theo] & nbsp; Bước 6-> 0 Thuật toán dừng & nbsp; Sơ đồ sau sẽ minh họa quá trình đệ quy & nbsp; & nbsp;
Sum of digits in 12345 is 151
using
namespace
std;
int
sum_of_digit[
int
Sum of digits in 12345 is 150
Sum of digits in 12345 is 152
Sum of digits in 12345 is 153
Sum of digits in 12345 is 154
if[n 1 % 10 bằng nhau-too 1 + [gửi 1/10 đến bước tiếp theo] & nbsp; Bước 6-> 0 Thuật toán dừng & nbsp; Sơ đồ sau sẽ minh họa quá trình đệ quy & nbsp; & nbsp;Sum of digits in 12345 is 151
using
namespace
std;
int
sum_of_digit[
int
Sum of digits in 12345 is 150Sum of digits in 12345 is 152Sum of digits in 12345 is 153Sum of digits in 12345 is 154if[n