reducing digits problem

This is a discussion on reducing digits problem within the C++ Programming forums, part of the General Programming Boards category; hey.i'm new with C++ and trying to start with simple coding. here's the problem. there's no error in my code ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    30

    Question reducing digits problem

    hey.i'm new with C++ and trying to start with simple coding. here's the problem. there's no error in my code but keeps getting a failed result and couldnt figure out why.here's some code i made.
    it suppose to give me
    reduceDigits(173) == 2 where 1x7x3=21 and 2x1 =2 (giving the least output)

    Code:
    unsigned int reduceDigits (unsigned int number)
    {
    	int digit = 0;
    	int product = 1;
    
    	if (number != 0){	
    		digit = number % 10;
    		product *= digit;
    		number = number / 10;	
    	}
    
    	else{
    		if (product > 9){
    			reduceDigits (product);
    		}
    		else{
    			cout << product;
    		}
    	}
    	return number;
    }
    could anyone point out where i made mistake in this code?
    thanks heaps.
    Last edited by Salem; 08-17-2009 at 11:36 AM. Reason: [CODE][/CODE] go around the code, not just slapped in anwhere to make the message go away

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,260
    First of all, you need to use [code] tags when posting code.

    Follow your code and look what it is doing.

    1. reduceDigits is called with a value of 173
    2. if(number != 0) is true
    3. digit is set to 3.
    4. product is set to 3
    5. number is set to 17.
    6. number (17) is returned.

    That is obviously the incorrect answer. You need to write out an algorithm on paper which can give you the correct answer. Do this before you write a single line of code. Once you have the algorithm down, then you can start worrying about coding issues.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "else" is where you've gone wrong (the first one, not the second one). Why would you only want to do all that if number==0?

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    isn't the return number should keep on looping in the if statement.it's like updating the old value for number.in this case instead of taking 173 it will take 17 as the new parameter.is it?

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    the number is suppose to be 17,1,0..which means the testing for the product only can be done until all the single number in the parameter is been multiply.well,that was i thought it should do.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,260
    Quote Originally Posted by effa View Post
    isn't the return number should keep on looping in the if statement.it's like updating the old value for number.in this case instead of taking 173 it will take 17 as the new parameter.is it?
    What loop? You don't have a loop anywhere in your code. You have recursion that will never get called due to the else that tabstop pointed out. Follow the code like I pointed out in my original post, and you will see what I mean.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. reverse a number digits
    By tootoo in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2007, 12:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21