Harsh Dhruv
Harsh Dhruv

Reputation: 3

Convert any number to one digit number

Consider the number 2345.If you multiply the digits of it then you get the number 120.Now if you again multiply the digits of 120 then you will get 0 which is one digit.

import java.util.Scanner;

public class SmallestNum 
{
    int prod=1,sum=0;
    void product(int m)
    {         
        while(m!=0)
        {
           int a=m%10;
           m=m/10;
           prod=prod*a;
        }   
       System.out.println(prod);            
    }


   public static void main(String args[])
   {
       Scanner scn=new Scanner(System.in);
       int x=scn.nextInt();
       SmallestNum sn=new SmallestNum();
       sn.product(x);
   }
}

I can get the 120 from this code.But how can i do the same procedure with 120 and get the answer 0.Pls help me.

Upvotes: 0

Views: 4579

Answers (5)

Guillaume
Guillaume

Reputation: 5555

You can just add an other loop around your while, the end condition being prod < 10, i.e. having only one number.

void product(int m)
{
    int prod;
    do {
        prod = 1;
        while(m!=0)
        {
           int a = m%10;
           m = m / 10;
           prod *= a;
        }   
        System.out.println(prod);
     } while (prod >= 10);
}

Upvotes: 1

3yakuya
3yakuya

Reputation: 2672

public int reduceToOneDigit(int inputNumber){
    int result = 1;
    while(inputNumber > 0){
        result *= (inputNumber % 10);
        inputNumber /= 10;
    }
    if (result > 9)
        result = reduceToOneDigit(result);
    return result;
}

So basically: multiply the digits of your inputNumber. If the result has more than one digit (so result is > 9, at least 10) call method recursively on the result.

Alternatively, do the same without recursion, using a do-while loop:

public int reduceToOneDigitNoRecursion(int inputNumber){
    int result = 1;
    do{
          while(inputNumber > 0){
              result *= (inputNumber % 10);
              inputNumber /= 10;
          }
    }
    while(result > 9);
    return result;
}

Upvotes: 0

irem
irem

Reputation: 3

make member function product return to an int. then instead of sn.product(x);

int p = sn.product(x);
while (p > 9)
{ 
    p = sn.product(t);
}

Upvotes: 0

Ronak Jain
Ronak Jain

Reputation: 2440

Recursive call the function

  if(String.valueOf(prod).length()>1){
       product(prod)
    } 

complete code

public class SmallestNum 
{
int prod=1,sum=0;
void product(int m)
{

        while(m!=0)
        {
           int a=m%10;
           m=m/10;
           prod=prod*a;
        }
        if(String.valueOf(prod).length()>1){
           product(prod)
        }
           System.out.println(prod);
}

public static void main(String args[])
{
   Scanner scn=new Scanner(System.in);
   int x=scn.nextInt();
   SmallestNum sn=new SmallestNum();
   sn.product(x);
}
}

Upvotes: 0

Aniket Thakur
Aniket Thakur

Reputation: 68935

Use recurssion

void product(int m)
{

        if(m%10 == 0){
        return;
        }

        while(m!=0)
        {
           int a=m%10;
           m=m/10;
           prod=prod*a;
        }

           System.out.println(prod);
           product(prod);//repeat the procedure
}

Upvotes: 0

Related Questions