sum combination

This is a discussion on sum combination within the C Programming forums, part of the General Programming Boards category; need to print all sum combination of integer; Code: void what(int sum) { int i; for(i=1; i<=sum; i++) { printf("%d",i); ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    sum combination

    need to print all sum combination of integer;

    Code:
    void what(int sum)
    {
    int i;
    	for(i=1; i<=sum; i++)
    	{
    		printf("%d",i);
    		what(sum-i);
    	}
    	puts("");
    }
    
    void main() 
    {
    
    	what(4);
      
    }

    what i should change ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Rather aptly, what is "all sum combination of integer"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    24
    Quote Originally Posted by laserlight View Post
    Rather aptly, what is "all sum combination of integer"?

    what(4)

    1111
    211
    22
    31
    4

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    1
    You could try some thing of this sort

    4 can be written this way

    1111
    Now you increment the first digit and decrement the last digit. Rest remain the same
    2110

    Do the same thing again
    2200

    Do the same thing again
    3100

    0 cannot be decremented so we cannot increment 1. Therefore the next one will be
    4000

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    24
    Quote Originally Posted by abhimanipal View Post
    you could try some thing of this sort

    4 can be written this way

    1111
    now you increment the first digit and decrement the last digit. Rest remain the same
    2110

    do the same thing again
    2200

    do the same thing again
    3100

    0 cannot be decremented so we cannot increment 1. Therefore the next one will be
    4000

    so ?

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by the_contractor View Post
    so ?
    That is an answer and not a bad one at that. He/she was trying not to do your work for you so they showed you logic that could lead you to all possible results.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Application Repeatition
    By lgcarter in forum C Programming
    Replies: 3
    Last Post: 06-16-2009, 02:07 PM
  2. Replies: 1
    Last Post: 05-28-2009, 01:28 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 07:40 AM
  4. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 05:43 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM

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