1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

please suggest me logic of the problem:

Discussion in 'C' started by Ankur Kamboj, Jun 19, 2011.

  1. Ankur Kamboj

    Ankur Kamboj New Member

    Joined:
    Jun 19, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Q:-find the all possible combination of digits ranging 1 to 9 whose sum is 10, no digit should be repeated in any combination.
    e.g:
    1234
    127
    136
    145
    19
    235
    28
    37
    46
     
  2. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    Hi,

    May this s not an optimised/generalise solution ..But it produces the output as expected
    The code snippet is as under:

    Code:
    #include<stdio.h>
    int main()
    {
      int i,j,k,l,sum;
      for(i = 1;i<10;i++)
      {
        sum = 0;
        for ( j=i+1 ;j<10; j++)
            {
             sum = i+j;
              if(sum == 10)
              {
                 printf("%d %d\n",i,j);
                 break;
               }
              if(sum > 10)
              break;
              for(k=j+1;k<10;k++)
              {
                sum = i+j+k;
               if(sum == 10)
               {
                printf("%d %d %d\n",i,j,k);
               break;
              }
              if(sum > 10)
             break;
    
                    for ( l=k+1;l<10;l++)
                    {
                        sum = i+j+k+l ;
                            if(sum == 10)
                            {
                            printf("%d %d %d %d \n",i,j,k,l);
                            break;
                            }
    
                    }
    
             }
        }
      }
     
    Last edited by a moderator: Jun 20, 2011
  3. Ankur Kamboj

    Ankur Kamboj New Member

    Joined:
    Jun 19, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    but i want it with least complexity like O(n)...
     

Share This Page