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:
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 <bits/stdc++.h>
Đư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 152Sum of digits in 12345 is 153 Sum of digits in 12345 is 154
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
Sum of digits in 12345 is 152Sum of digits in 12345 is 156 Sum of digits in 12345 is 157
Sum of digits in 12345 is 152Sum of digits in 12345 is 156 if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 0
int if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 3
Sum of digits in 12345 is 151
Sum of digits in 12345 is 152#include <bits/stdc++.h>2#include <bits/stdc++.h>3#include <bits/stdc++.h>4
#include <bits/stdc++.h>5#include <bits/stdc++.h>6#include <bits/stdc++.h>7#include <bits/stdc++.h>8
int sum_of_digit(int Sum of digits in 12345 is 150
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
Sum of digits in 12345 is 152Sum of digits in 12345 is 153 Sum of digits in 12345 is 154
using3
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 152Sum of digits in 12345 is 153 Sum of digits in 12345 is 154
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
Sum of digits in 12345 is 152Sum of digits in 12345 is 156 Sum of digits in 12345 is 157
Sum of digits in 12345 is 151
int if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 3
Sum of digits in 12345 is 151
Sum of digits in 12345 is 152std;9int0int1int2
int sum_of_digit(int Sum of digits in 12345 is 150
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
Sum of digits in 12345 is 152Sum of digits in 12345 is 153 Sum of digits in 12345 is 154
Sum of digits in 12345 is 152Sum of digits in 12345 is 156 Sum of digits in 12345 is 157
Sum of digits in 12345 is 152Sum of digits in 12345 is 156 if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 0
Sum of digits in 12345 is 151
int if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 3
Sum of digits in 12345 is 152Sum of digits in 12345 is 151
Sum of digits in 12345 is 151
Sum of digits in 12345 is 152int if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 7
Sum of digits in 12345 is 152int #include <bits/stdc++.h>0
Sum of digits in 12345 is 152if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
C
Sum of digits in 12345 is 152Sum of digits in 12345 is 151
#include <bits/stdc++.h>5Sum of digits in 12345 is 156 Sum of digits in 12345 is 157
Java
int7 int8
int9 sum_of_digit(0
Sum of digits in 12345 is 152if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
Python3
Sum of digits in 12345 is 152sum_of_digit(3 int sum_of_digit(int Sum of digits in 12345 is 150
int0Sum of digits in 12345 is 153 int2int3int4
int5Sum of digits in 12345 is 156 int3int8
‘
Sum of digits in 12345 is 152Sum of digits in 12345 is 1509 sum_of_digit(3 Sum of digits in 12345 is 1511 Sum of digits in 12345 is 1512
int0int Sum of digits in 12345 is 1517Sum of digits in 12345 is 1518int8
Sum of digits in 12345 is 1564int0Sum of digits in 12345 is 1566Sum of digits in 12345 is 1567Sum of digits in 12345 is 1568Sum of digits in 12345 is 1569
C#
int0int #include <bits/stdc++.h>0
int0Sum of digits in 12345 is 1524#include <bits/stdc++.h>3 Sum of digits in 12345 is 1526
int if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 3
Sum of digits in 12345 is 152Sum of digits in 12345 is 151
Sum of digits in 12345 is 151
Sum of digits in 12345 is 152int if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 7
Sum of digits in 12345 is 152int #include <bits/stdc++.h>0
Sum of digits in 12345 is 152if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
C
Sum of digits in 12345 is 152Sum of digits in 12345 is 151
#include <bits/stdc++.h>5Sum of digits in 12345 is 156 Sum of digits in 12345 is 157
Java
int7 int8
int9 sum_of_digit(0
Sum of digits in 12345 is 152if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
Sum of digits in 12345 is 152sum_of_digit(3 int sum_of_digit(int Sum of digits in 12345 is 150
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 17
int0Sum of digits in 12345 is 153 int2int3int4
Sum of digits in 12345 is 151
int5Sum of digits in 12345 is 156 int3int8
‘
Sum of digits in 12345 is 152Sum of digits in 12345 is 1509 sum_of_digit(3 Sum of digits in 12345 is 1511 Sum of digits in 12345 is 1512
int0int Sum of digits in 12345 is 1517Sum of digits in 12345 is 1518int8
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
int0int #include <bits/stdc++.h>0
int0Sum of digits in 12345 is 1524#include <bits/stdc++.h>3 Sum of digits in 12345 is 1526
Sum of digits in 12345 is 1527Sum of digits in 12345 is 1528#include <bits/stdc++.h>7 Sum of digits in 12345 is 1530
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 57
Sum of digits in 12345 is 1534 Sum of digits in 12345 is 1535
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 58
Sum of digits in 12345 is 152Sum of digits in 12345 is 153 Sum of digits in 12345 is 1538Sum of digits in 12345 is 1539Sum of digits in 12345 is 1539 int3Sum of digits in 12345 is 1542
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 152Sum of digits in 12345 is 153 Sum of digits in 12345 is 154
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 1
Sum of digits in 12345 is 152Sum of digits in 12345 is 156 Sum of digits in 12345 is 157
Sum of digits in 12345 is 152Sum of digits in 12345 is 156 if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 0
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 76#include <bits/stdc++.h>3if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 78
int if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 3
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 83
Output:
Sum of digits in 12345 is 15Sum of digits in 12345 is 151
if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10Sum of digits in 12345 is 152int if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 7O(logn) where n is the given number.
Sum of digits in 12345 is 152int #include <bits/stdc++.h>0O(logn) due to recursive stack space.