/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/SumOfDigits.java |
package edu.slcc.asdv.caleb.lab5.recursion2_calebfontenot;
import java.util.Scanner;
@author
public class SumOfDigits {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int i = input.nextInt();
System.out.print("The sum of digits in " + i
+ " is " + sumDigits(i));
System.out.println();
}
public static long sumDigits(long n)
{
if (n < 10) {
return n;
} else {
long lastDigit = n % 10;
long remainingDigits = n / 10;
return lastDigit + sumDigits(remainingDigits);
}
}
}