-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvancedClassificationRecursion.c
More file actions
77 lines (61 loc) · 1.79 KB
/
advancedClassificationRecursion.c
File metadata and controls
77 lines (61 loc) · 1.79 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
#include <stdio.h>
#include "NumClass.h"
// Function to calculate the power of a number
int power(int base, int exponent) {
if (exponent == 0) {
return 1;
}
else{
return base * power(base, exponent - 1);
}
}
// Function to calculate the number of digits in an integer
int countDigits(int num) {
if (num == 0) {
return 0;
} else{
return 1 + countDigits(num / 10);
}
}
// Function to check if a number is an Armstrong number recursively
int isArmstrongRecursive(int num, int originalNum, int n) {
if (num == 0) {
return 0;
} else {
return power(num % 10, n) + isArmstrongRecursive(num / 10, originalNum, n);
}
}
// Function to check if a number is an Armstrong number recursively
int isArmstrong(int num) {
// Find the number of digits in the given number
int n = countDigits(num);
// Calculate the sum of nth powers of digits recursively
int sum = isArmstrongRecursive(num, num, n);
// Check if the sum is equal to the original number
return sum == num;
}
// Function to check if an integer is a palindrome recursively
int isPalindrome(int num) {
if (num < 0) {
// Negative numbers are not palindromic
return 0;
}
int digits = countDigits(num);
if (digits <= 1) {
// Single-digit numbers are palindromic
return 1;
}
int divisor = 1;
for (int i = 1; i < digits; ++i) {
divisor *= 10;
}
int firstDigit = num / divisor;
int lastDigit = num % 10;
if (firstDigit != lastDigit) {
// If the first and last digits are not equal, it's not a palindrome
return 0;
}
// Recursively check the palindrome status of the remaining sub-number
int remainingNum = (num % divisor) / 10;
return isPalindrome(remainingNum);
}