Learn-dsa..in 30 days!



























CC-19 : Find sum of digits of number.

Description:

Given an input num N find sum of its digits.

Test cases and expected outputs:

Input Parameters Expected outputs
Number: 5432 Sum of digits: 14
Number: 678 Sum of digits: 21

Pseudocode:

sumDigits(N):

If N==0:
return 0.
Else :
o Return number calculated by adding num%10 to return value from sumDigits(num/10).

Code:

public int sumDigits(int num){
	if (num == 0) {
		return 0;
	}else {
		int sum= num%10 + sumDigits(num/10);
		return sum;
	}
}

Click here to download and run code and test cases !