Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9195037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:32:52+00:00 2026-06-17T21:32:52+00:00

I need some help checking over my program to find the digit root. If

  • 0

I need some help checking over my program to find the digit root. If a user enters 5635 the digit root is 1. To find the digit root of a number you add all the digits in the number 5 + 6 + 3 + 5 and you get the result of 19. Then you add the result 1 + 9 = 10. Then you add 1 + 0 until you get 1 which is your digit root.

  1. Do i have the right approach or is my approach to the question totally off?
  2. Why am i getting 0 as the result instead of the correct answer of 1?
import acm.program.*; 

public class DigitRoot extends ConsoleProgram {

    public void run() {
        println("this program attemts to find the digit root a user enters.");

        int n = readInt("please enter any positive integer: ");

        int dsum = 0;
        int sumtotal = 0;
        int threesum = 0;
        int foursum = 0;

        while (n > 0) {
            dsum += n % 10;
            n /= 10;

            if (dsum > 9) {
                sumtotal = (dsum / 10) + (dsum % 10);
            } else if (sumtotal > 9) {
                threesum = (sumtotal / 10) + (sumtotal % 10);
            } else if (threesum > 9) {
                foursum = (threesum / 10) + (threesum % 10);
            } else if (foursum < 9) {
                println("your digit root is" + foursum);
            } else {
                println("this program is borken.");
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:32:53+00:00Added an answer on June 17, 2026 at 9:32 pm

    Example using a recursion – I calculate the sum of the figures by using a String: it is not very efficient but is (slightly) easier. Feel free to improve by using n % 10 in a loop.

    public static void main(String[] args) throws Exception {
        System.out.println(calculateRoot(5635)); //prints 1
    }
    
    public static int calculateRoot(int n) {
        int sum = sumFigures(n);
        return sum < 10 ? sum : calculateRoot(sum);
    }
    
    public static int sumFigures(int n) {
        String s = String.valueOf(n);
        int sum = 0;
        for (char c : s.toCharArray()) {
            sum += c - '0';
        }
        return sum;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query I need some help with, have been checking out a
I need some help in adding the replace all functionality into my Flex project.
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help with Activerecord Querying in a has_many :through association. Model: Job class
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help assigning a mouseover event to display some icons that start out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.