-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursionExercise.cpp
More file actions
91 lines (61 loc) · 1.03 KB
/
Copy pathrecursionExercise.cpp
File metadata and controls
91 lines (61 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/****************************
* Name: Julian Moskovits
* Description: Recursive exercises.
*
****************************/
#include <iostream>
#include <cstdlib>
using namespace std;
//Calculates x^n.
int exp(int x, int n){
if (n == 1){
return x;
}
return x * exp(x, n - 1);
}
//Prints a string.
void print(char* s){
if (*s == '\0'){
return;
}
cout << *s;
print(++s);
}
//Prints the string backwards.
void rev_print(char* s){
if (*s == '\0'){
return;
}
rev_print(s + 1);
cout << *s;
}
//Prints an integer backwards.
void revPrintInt(int n){
if (n / 10 == 0){
cout << n % 10;
return;
}
cout << n % 10;
revPrintInt(n / 10);
}
//Prints an integer.
void printInt(int n){
if (n / 10 == 0){
cout << n % 10;
return;
}
printInt(n / 10);
cout << n % 10;
}
int main()
{
int base = 3, expo = 4;
char str[] = "hello";
int n = 123456789;
cout << exp(base, expo) << endl;
print(str); cout << endl;
rev_print(str); cout << endl;
revPrintInt(n); cout << endl;
printInt(n); cout << endl;
return 0;
}