math/sum_of_digits/ruby/sum_of_digits.rb
# Function to calculate the sum of digits
Function to calculate the sum of digits
Function to calculate the sum of digits
def sum_of_digits (num = 0)
	sum = 0
	while (num != 0)
		digit = num % 10	
		num /= 10		
		sum += digit		
	end
	return sum
end
def sum_of_digits ( num = 0 ) sum = 0 while ( num != 0 ) digit = num % 10 num /= 10 sum += digit end return sum end
Function to calculate the sum of digits
Function to calculate the sum of digits
######################################################################################################################### #The Algorithm for finding the sum_of_digits(num) of a given number is as follows: # If num = 0 then sum_of_digits(num) = 0 # Else  # extract the last digit of num # add the last digit to the total sum  # drop the last digit of the number # Repeat until num is not equal to 0 ######################################################################################################################### # To extract the last digit of the number # Drop the last digit of the number  # Add the digit to the total sum
num
<not_specific>