Advertisement
ULK

РЕКУРСИЯ

ULK
May 3rd, 2023 (edited)
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void pow2(int n) {
  5.     if (n >= 2) {
  6.         if (n % 2 == 0) {
  7.             n /= 2;
  8.             cout << " " << n;
  9.             pow2(n);
  10.         }
  11.         else cout << "\n no <3" << endl;
  12.  
  13.         if (n == 2) {
  14.             cout << "\n yasss" << endl;
  15.         }
  16.     }
  17. }
  18.  
  19. int t = 0;
  20. void digit_sum(int m) {
  21.  
  22.     t += m % 10;
  23.     m /= 10;
  24.  
  25.     if (m > 9) {
  26.         digit_sum(m);
  27.     }
  28.     else {
  29.         cout << t + m;
  30.     }
  31.  
  32. }
  33.  
  34. void reverse(int k) {
  35.  
  36.     cout << " " << k % 10;
  37.     k /= 10;
  38.  
  39.     if (k > 9) {
  40.         reverse(k);
  41.     }
  42.     else {
  43.         cout << " " << k;
  44.     }
  45. }
  46.  
  47. int k = 0;
  48. void row(int j) {
  49.  
  50.     k++;
  51.  
  52.     if (j != 1) {
  53.         for (int i = 0; i < k; i++) {
  54.             cout << " " << k;
  55.             if (i == j) exit(0);
  56.             else if (i + 1 == k and k != j) {
  57.                 row(j);
  58.                 exit(0);
  59.             }
  60.  
  61.         }
  62.     }
  63. }
  64.  
  65. int main() {
  66.  
  67.     int x;
  68.     cout << "choose the program (1-4): " << endl;
  69.     cin >> x;
  70.  
  71.     switch (x) {
  72.         case 1:
  73.             cout << "enter the number(pow2): " << endl;
  74.             int n;
  75.             cin >> n;
  76.             pow2(n);
  77.             break;
  78.  
  79.         case 2:
  80.             cout << "\n enter the number(digit_sum): " << endl;
  81.             int m;
  82.             cin >> m;
  83.             digit_sum(m);
  84.             break;
  85.  
  86.         case 3:
  87.             cout << "\n enter the number(reverse): " << endl;
  88.             int k;
  89.             cin >> k;
  90.             reverse(k);
  91.             break;
  92.  
  93.         case 4:
  94.             cout << "\n enter the number(row): " << endl;
  95.             int j;
  96.             cin >> j;
  97.             row(j);
  98.             break;
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement