Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Returning wrong value without modifying!!

  1. #1
    Junior Member
    Join Date
    Aug 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Returning wrong value without modifying!!

    Iam trying to solve a question.The question is
    Finding sum of digits of a number until sum becomes single digit.
    .
    Example
    Input : 12345
    Output: 6

    The individual digits should be added untill it become single digit.1+2+3+4+5=15 -->1+5=6.

    In the below code the input is:
    i)first line number of test cases
    ii)second line is the number.

    When I run the code , the following are the inputs Iam giving:
    1;
    12345;
    The output should be "6" but it is printing "15".It is not printing "6".Iam unable to understand what's wrong woth the below code.Plz. help!!

    import java.util.Scanner;
    class AddNum{
     
    	public int  Add(int num) {
    		int x;
    		int sum=0;
    		int rem;
     		while(num>0) {
     
    			rem=num%10;
    			sum=sum+rem;
    			num/=10;
    		}
    		if(sum>9) {
    			Add(sum);
    		}
    		return sum;
     
     
    	}	
    }
     class IndividSum {
     
    public static void main(String[]  args)throws Exception{
    	Scanner sc=new Scanner(System.in);
    	int test=sc.nextInt();
    	AddNum obj=new AddNum();
    	int num[]=new int[test];
    	int Ans[]=new int[test];
    	for(int i=0;i<test;i++) {
    		num[i]=sc.nextInt();
    	}
    	for(int i=0;i<test;i++) {
    		Ans[i]= obj.Add(num[i]);
    		System.out.println(Ans[i]);
     
    	}
    sc.close();
    }
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Returning wrong value without modifying!!

    How are you debugging the code to see where it is going wrong?

    Add some print statements that print out the values of variables as the code changes their values and uses them. The print out should show you what the computer sees when it executes and help you find the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Returning wrong value without modifying!!

    Actually the sum value in class AddNum is 6 but when the value is returned to Ans[i] in class IndividSum,it is showing 15.My question is why is it returning wrong value?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Returning wrong value without modifying!!

    why is it returning wrong value?
    You need to debug the code to see what it is doing so you can find the problem.
    I suggested adding print statements to the code so you can see what it is doing. The print out will help you find the problem.

    Can you copy the full contents of the debug printout that shows what the code is doing and paste it here?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member tonya's Avatar
    Join Date
    Feb 2018
    Posts
    16
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Returning wrong value without modifying!!

    the problem is in your recursion, when it backs out sum is the old value, you need to change your recursive call to:
    if(sum>9) {
    	sum = Add(sum);
    }

Similar Threads

  1. Program returning wrong values.
    By cam25 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 11th, 2012, 11:59 PM
  2. Help with modifying a program for evaluating arithmetic expressions
    By rockout341 in forum Object Oriented Programming
    Replies: 5
    Last Post: October 13th, 2011, 11:01 AM
  3. values of variables changing despite not modifying them
    By AndyKow in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2011, 12:39 PM
  4. Modifying a complicated object with dialog boxes
    By hunter555 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 27th, 2011, 03:02 PM
  5. Interating through an ArrayOrderedList and modifying elements within
    By xecure in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 26th, 2010, 11:15 PM