Add digits of number in c
Add digits of number in c
#include <stdio.h> int main() { int n, t, sum = 0, remainder; printf("Enter an integer\n"); scanf("%d", &n); t = n; while (t != 0) { remainder = t % 10; sum = sum + remainder; t = t / 10; } printf("Sum of digits of %d = %d\n", n, sum); return 0; }
Add digits using recursion
#include <stdio.h> int add_digits(int); int main() { int n, result; scanf("%d", &n); result = add_digits(n); printf("%d\n", result); return 0; } int add_digits(int n) { static int sum = 0; if (n == 0) { return 0; } sum = n%10 + add_digits(n/10); return sum; }
Comments
Post a Comment