-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvancedClassificationRecursion.c
More file actions
48 lines (44 loc) · 1.12 KB
/
Copy pathadvancedClassificationRecursion.c
File metadata and controls
48 lines (44 loc) · 1.12 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
#include "NumClass.h"
// Calculate the pow recursively
int powRec(int n , int x) {
if (x == 0) {
return 1;
}else {
return n * powRec(n , x-1);
}
}
// Calculate the length of the number recursively
int lengthRec(int x) {
if (x == 0) {
return 0;
} else {
return 1 + lengthRec(x / 10);
}
}
int sumOfPowers(int x, int lenOfNumber) {
if (x == 0) {
return 0;
} else {
int digit = x % 10;
return powRec(digit, lenOfNumber) + sumOfPowers(x / 10, lenOfNumber);
}
}
// Function to check if a number is an Armstrong number recursively
int isArmstrongRec(int x, int lenOfNumber) {
return (x == sumOfPowers(x, lenOfNumber));
}
int isArmstrong(int x){
return isArmstrongRec(x , lengthRec(x));
}
// Recursive function for Palindrome
int isPalindromeRec(int x, int originalNum) {
if (x == 0) {
return originalNum;
} else {
return isPalindromeRec(x / 10, originalNum * 10 + x % 10);
}
}
// Wrapper function to call the recursive function send to "isPalindromeRec"
int isPalindrome(int x) {
return (x == isPalindromeRec(x, 0));
}