so I am learning C, and for one of the excercises the website I am learning on, I am supposed to make a program with a recursive function that calculates the sum of the digits of an integer. When using VS code, my program works perfectly every time (to my knowledge, which is slight), but when I paste it into the website's editor, it says that its calculations are wrong. Here is my code:
Code:
Thank you in advance!
Code:
#include <stdio.h>
#include <stdlib.h>
int sumOfDigits(int);
int getLastDigit(int);
int popLastDigit(int);
int main() {
int number;
int sum;
scanf("%d", &number);
sum = sumOfDigits(number);
printf("%d", sum);
return 0;
}
int sumOfDigits(int num) {
int curDigit;
int result = 0;
if (num>9) {
curDigit = getLastDigit(num);
num = popLastDigit(num);
result += curDigit + ( sumOfDigits(num) );
return result;
} else {
return num;
}
}
int getLastDigit(int num) {
int result;
result = num%10;
return result;
}
int popLastDigit(int num) {
int result;
int clippedDigit;
if (num>9) {
clippedDigit = getLastDigit(num);
result = (num - clippedDigit) / 10;
return result;
} else {
return 0;
}
}
Thank you in advance!