find sum of digits code example

Example 1: sum of digits

num = [int(d) for d in input("Enter the Number:")]
sum = 0
for i in range(0, len(num)):
    sum = sum + num[i]

print("Sum of Digits of a Number: {}".format(sum))

# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student)

Example 2: sum the digits of an integer

#include <stdio.h>
 
int getSum(unsigned long long n) {
    int sum = 0;
    for (; n; n /= 10)
    	sum += n % 10;
    return sum;
}

int main(){
	printf("Sum of digits = %d\n", getSum(555));
}