Jessica M.
Jessica M.

Reputation: 1459

Finding the digit root of a number

Study question is to find the digit root of a already provided number. The teacher provides us with the number 2638. In order to find the digit root you have to add each digit separately 2 + 6 + 3 + 8 = 19. Then you take the result 19 and add those two digits together 1 + 9 = 10. Do the same thing again 1 + 0 = 1. The digit root is 1.

My first step was to use the variable total to add up the number 2638 to find the total of 19. Then I tried to use the second while loop to separate the two digits by using the %

I have to try and solve the problem by using basic integer arithmetic (+, -, *, /).
1.Is it necessary and or possible to solve the problem using nested while loops?
2.Is my math correct?
3. As I wrote it here it does not run in Eclipse. Am I using the while loops correctly?

import acm.program.*;


public class Ch4Q7 extends ConsoleProgram {
   public void run(){
      println("This program attempts to find the digit root of your number: ");

      int n = readInt("Please enter your number: "); 

      int total = 0;

      int root = total;


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

    }   

   while ( total > 0 ){
     root = total; 
     total = ((total % 10) + total / 10); 
    }


     println("your root should be " + root);
}

}

Upvotes: 1

Views: 3962

Answers (7)

user93353
user93353

Reputation: 14039

  int root = 0;
  int t = 0;

  while (true)
  {
    root = root + (n %10);
    t = n/10;
    n = t;

    if(t == 0)
    {
        if(root < 10)
            break;

        n = root;
        root = 0;
    }
}   

or even simpler

int root = 0;

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

if((root == 0) || (root%9 != 0))
    root = root%9;
else
    root = 9;        

Upvotes: 0

Ofir Luzon
Ofir Luzon

Reputation: 10908

You can sum only last digit until you get a one digit number:

public static int getRoot(int n) {
    int root=n;
    while ( (root=((root%10) + root/10))>9  );
    return root;
}  

Or with recursion:

public static int recursionGetRoot(int n) {
    if(n<10)
        return n;
    return n%10 + recursionGetRoot(n/10);
}  

Upvotes: 1

Vala
Vala

Reputation: 5674

A recursive solution:

public static void main(String[] args)
{
    System.out.println(getDigitRoot(1));
    System.out.println(getDigitRoot(11));
    System.out.println(getDigitRoot(1924));
    System.out.println(getDigitRoot(2638));
}

public static int getDigitRoot(int n)
{
    if (n < 10)
    {
        return n;
    }
    int sum = 0;
    while (n > 0)
    {
        sum += n % 10;
        n = n / 10;
    }
    return getDigitRoot(sum);
}

Outputs:

1
2
7
1

I'm of two minds about recursion in this one. It more closely follows what you're doing when manually solving it and as such makes sense, however it's not too hard to implement iteratively and as usual that's more scalable. I think in most cases it won't matter since performance is unlikely to be critical and you're unlikely to deal with numbers large enough to cause a problem for recursion.

Upvotes: 1

Bhavik Shah
Bhavik Shah

Reputation: 5183

2.Is my math correct? Yes 3. As I wrote it here it does not run in Eclipse. Am I using the while loops correctly? No 1.Is it necessary and or possible to solve the problem using nested while loops?

Yes it is possible using nested loops as the answers given by fvu above

do {
    while (n > 0) {
        total = total + (n % 10);
        n = (n / 10);
    }
    n = total;
    total = 0;
} while (n > 9); 

Is it necessary using nested loop. No you can use recursion

public static void run(){
      System.out.println("This program attempts to find the digit root of your number: ");

      //int n = 234567; 

      int total = 23456;

    //  int root = total;


          total=Test.calctotal(total);

      System.out.println("Root:"+total);
}
public static int calctotal(int n)
{
    int total=0;
    System.out.println(n);
    if(n>9)
    {
        while (n > 0 ){
            total = total + (n %10);
            n = (n / 10);
        }  
        total=Test.calctotal(total);
    }
            else
                total=n;
    return total;
}

Upvotes: 0

fvu
fvu

Reputation: 32953

I think it does run, but just a bit too much :-)

total = ((total % 10) + total / 10); 

can't converge to 0. Also, your program as it is will only handle very specific cases. As others pointed out, this can be solved recursively, but also with just a double loop. A succession of loops like you tried won't work.

Try this (on input vars are the same as in your program, it's really a plugin replacement for your two loops):

    do {
        while (n > 0) {
            total = total + (n % 10);
            n = (n / 10);
        }
        n = total;
        total = 0;
    } while (n > 9);  // need at least 1 more loop

After this loop n will contain the root number.

Upvotes: 2

Andreas Dolk
Andreas Dolk

Reputation: 114767

Why don't you test it yourself? Here's a very simple way to write some test (e.g. comparisions between actual results and expected results) and the math is correct if all those tests pass.

First we need to extract the math in a separate method, because we need something that takes input and provides output:

public void run(){
  println("This program attempts to find the digit root of your number: ");

  int n = readInt("Please enter your number: "); 

  int root = calculateRoot(n);    

  println("your total should be " + root);
}

public static int calculateRoot(int n) {
  int total = 0;
  int root = total;
  while (n > 0 ){
    total = total + (n %10);
    n = (n / 10);
  }   

  while ( total > 0 ){
    root = total; 
    total = ((total % 10) + total / 10); 
  }

  return root;
}

And with that at hand, we can create a main method to execute some tests:

public static void main(String[] args) {

  if (0 == calculateRoot(0)) {
    System.out.println("0: OK");
  }

  if (1234 == calculateRoot(10)) {
    System.out.println("1234: OK");
  }

  // and so on

}

So just implement your algorithm and execute the class and verify by the output, that all tests are OK. This is VERY simplified and later you'll use special test tools but the general approach will be the same: define some test cases and code until all the implementation passes all tests.

Upvotes: 0

Tobias Ritzau
Tobias Ritzau

Reputation: 3327

Make a loop to check if your number is more than or equal to 10, i.e. it has more than one digit. Do your digit addition in an inner loop. Print the your intermediate results to check whether your math is correct, i.e. print the digits you extract and the sums. You can remove this later if you like. When your number is less than 10, you have your root.

Upvotes: 0

Related Questions