/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/SumOfDigits.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package edu.slcc.asdv.caleb.lab5.recursion2_calebfontenot;

import java.util.Scanner;

/**
 *
 * @author caleb
 */
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)
    {
        // Base case: if the number is a single digit, return it
        if (n < 10) {
            return n;
        } else {
            // Recursive case: sum the last digit and call the function on the remaining digits
            long lastDigit = n % 10;
            long remainingDigits = n / 10;
            return lastDigit + sumDigits(remainingDigits);
        }
    }
}