fork download
  1. #include <iostream>
  2.  
  3. #include <string>
  4.  
  5. using namespace std;
  6. // n > 0 task 12 4-th of April print all digits of numder
  7.  
  8. string digitsrec(int n){
  9. if (n == 0) return "";
  10. return digitsrec(n / 10) + " " + to_string(n % 10);
  11. }
  12.  
  13. string digitsloop(int n){
  14. string res = "";
  15. while (n > 0){
  16. res = " " + to_string(n % 10) + res;
  17. n = n / 10;
  18. }
  19. return res;
  20. }
  21.  
  22. int main() {
  23. int n;
  24. cin >> n;
  25.  
  26. cout << " Using digitsrec" << endl;
  27. cout << " for n = " << n << "\n digits" << endl;
  28. cout << digitsrec(n) << endl;
  29.  
  30. cout << " Using digitsloop" << endl;
  31. cout << " for n = " << n << "\n digits" << endl;
  32. cout << digitsloop(n) << endl;
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 5308KB
stdin
123456
stdout
 Using digitsrec
 for n = 123456
 digits
 1 2 3 4 5 6
 Using digitsloop
 for n = 123456
 digits
 1 2 3 4 5 6