-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path01_2048_problem_recursion.cpp
59 lines (47 loc) · 1.05 KB
/
01_2048_problem_recursion.cpp
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
/*
Topic - 2048 Problem Recursion
Number to Spelling
Input : 2048
Output : two zero four eight
APPRAOCH:
- first make a recursive call (n/10) (i.e n/10 reducing number)
- then print spelling of last digit (n%10) (i.e n%10 last digit)
*/
#include <iostream>
using namespace std;
// 2D character array of words as per index
char words[][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
void printSpellings(int num)
{
// base case
if(num == 0)
{
return;
}
// rec case
printSpellings(num/10);
// after the function call print spellings
int last_digit = num%10;
cout << words[last_digit] << " ";
return;
}
// function to drive code
int main()
{
int num;
cout << "Enter number: ";
cin >> num;
cout << "Output: ";
printSpellings(num);
cout << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter number: 2048
Output: two zero four eight
Case 2:
Enter number: 189642
Output: one eight nine six four two
*/