Click here to Skip to main content
16,022,295 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Question:

Complete the program which takes date in number format and then makes its addition of the digits until the
last digit remains single and the print the number as lucky number . eg if input is 777
then 7+7+7 =21 = 2+1 =3 so the answer is 3.

Example1:
Enter the date(ddmmyy):
131083
Your lucky number is:
7
Example2:
Enter the date(ddmmyy):
111111
Your lucky number is:
6

What I have tried:

i tried

Java
<pre>import java.io.*;
class digitsum{ 

public static void main(String arg[])throws Exception
{

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the date(ddmmyy):");
    int number =Integer.parseInt(br.readLine());


    System.out.println("Your lucky number is:"); 
    //doSum(number);
    System.out.println(doSum(number));
    
   
}//main
    static int doSum(int num)
    {
        int ans=0;
        if(num%10==0)
        {
            return num;
        }
        else
        {
            ans = num%10 + doSum(num/10);
        }
        if(ans<10)
        {
            return ans;
        }
        else
        {
            doSum(ans);
        }
       
        return ans;
    }
}  



this code works fine but
for an 777 input it showing 21
but i want
7+7+7 = 21 = 2+1 = "3" as final ans
Posted
Updated 8-Mar-18 12:34pm
v2

You should return a doSum call:
Java
static int doSum(int n)
{
  if (n <10)
    return n;
  return doSum( n%10 + n/10);
}
 
Share this answer
 
Comments
[no name] 8-Mar-18 17:32pm    
My mistake.
CPallini 9-Mar-18 3:10am    
By the way, thank you. :-)
[no name] 9-Mar-18 3:24am    
Last night I gave you the +5, then ran the code, and it gave the wrong answer. This morning I ran it again and it gave the right answer. :)
CPallini 9-Mar-18 3:43am    
You have a quantum computer, don't you? :-)
Java
static int doSum(int num)
{
    int ans=0;
    if(num%10==0) // wrong test try with 7770
    {
        return num;
    }
    else
    {
        ans = num%10 + doSum(num/10);
    }
    if(ans<10)
    {
        return ans;
    }
    else
    {
        doSum(ans); // Here you loose the result of doSum
    }

    return ans;
}


There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900