I am a beginner trying to write a program for my intro C class. I am trying to write a program that calculates the combination of numbers given a specific output by the user. The number has to be between 1-10 to be valid. I am not allowed to use the built in library that calculates a combination. I am getting stuck at the part where i need to calculate (n-k)! I see that my problem is after my loops the numbers the user inputs for n and k are now zero. I am not sure if I should have written this a different way or how I should go about getting the original entered digits back so that I can take and use them in calculating (n-k)! Any help would be very much appreciated.

int main()        /* Main declaration with arguments           */
{

int n,
    nFact = 1,
    k,
    kFact = 1,
    n_k = ( n - k ),
    nkFact = 1,
    combination;
 
 
   
do {
 printf("\nEnter the number of items in the list (n): ");
 scanf("%d", &n);
       if ( n < 1 || n > 10 ) /* is n between 1 - 10 */
          printf("\n?Invalid input: Number must be between 1 and 10\n");
       else  
          break;
} while ( n > 1 || n < 10 );
 
 
 
  /* As long as n>=1 program will loop the following n*n-1 */
  while (n>=1)
  {
       nFact = ( nFact * n );
       n = ( n - 1 );
  }    
  printf("n! = %d", nFact);
 

 
do {
 printf("\nEnter the number of items to choose (k): ");
 scanf("%d", &k);    
       if ( k < 1 || k > 10 )  /* is k between 1 - 10 */
          printf("\n?Invalid input: Number must be between 1 and 10\n");
       else  
          break;
} while ( k > 1 || n < 10 );





  while (k>=1)
  {
       kFact = ( kFact * k );
       k = ( k - 1 );
  }    
  printf("k! = %d", kFact);




  while (n_k>=1)
  {
        nkFact = ( nkFact * n_k );
        n_k = ( n_k - 1 );
  }    
printf("\n(n-k)! = %d", nkFact);


printf("\n");
system("PAUSE");
return(0);                                  
}

I am a beginner trying to write a program for my intro C class. I am trying to write a program that calculates the combination of numbers given a specific output by the user. The number has to be between 1-10 to be valid. I am not allowed to use the built in library that calculates a combination. I am getting stuck at the part where i need to calculate (n-k)! I see that my problem is after my loops the numbers the user inputs for n and k are now zero. I am not sure if I should have written this a different way or how I should go about getting the original entered digits back so that I can take and use them in calculating (n-k)! Any help would be very much appreciated.

int main()        /* Main declaration with arguments           */
{

int n,
    nFact = 1,
    k,
    kFact = 1,
    n_k = ( n - k ),
    nkFact = 1,
    combination;
   
do {
 printf("\nEnter the number of items in the list (n): ");
 scanf("%d", &n);
       if ( n < 1 || n > 10 ) /* is n between 1 - 10 */
          printf("\n?Invalid input: Number must be between 1 and 10\n");
       else  
          break;
} while ( n > 1 || n < 10 );
//the while test is goofed. If n < 1 || n > 10, THEN you want to loop back.
 
  /* As long as n>=1 program will loop the following n*n-1 */
  while (n>=1)
  {
       nFact = ( nFact * n );
       n = ( n - 1 );
  }    
  printf("n! = %d", nFact);
 
do {
 printf("\nEnter the number of items to choose (k): ");
 scanf("%d", &k);    
       if ( k < 1 || k > 10 )  /* is k between 1 - 10 */
          printf("\n?Invalid input: Number must be between 1 and 10\n");
       else  
          break;
} while ( k > 1 || n < 10 );

  while (k>=1)
  {
       kFact = ( kFact * k );
       k = ( k - 1 );
  }    
  printf("k! = %d", kFact);

  while (n_k>=1)
  {
        nkFact = ( nkFact * n_k );
        n_k = ( n_k - 1 );
  }    
printf("\n(n-k)! = %d", nkFact);

printf("\n");
system("PAUSE");
return(0);                                  
}

It looks like you're solving for the number of permutations, rather than combinations (which are far fewer). Can you confirm which it is?

Giving your variables better names than: nfact, kfact, nkfact, k, etc., would be very helpful.

Always highlight your program, and click on the # symbol, so your code, is punctuated to look like code, on this (and any other code), forum.

Sorry about that. I need to solve the equation:

combinations = n! / ((n-k)!*k!)

I am trying to break it down into its parts so I am first trying to solve for:

n!
k!
(n-k)!

the last is where I am getting stuck. I am not sure how to have n and k be equal to their original values instead of 0. Sorry for the confusion I am still very new, let me know if I am approaching this problem incorrectly.

#include <stdio.h>           /* Standard library for C input/output                */
#include <stdlib.h>        /* Standard library definitions                       */
#define TERMINAL -9        /* This is a macro that is defined globally           */
                           /* it is used to tell the program that the user is    */
                           /* done with their input.                             */

int main()        /* Main declaration with arguments           */
{

int userInput1,
    userInput2,
    nFact = 1,
    kFact = 1,
    n_k,
    nkFact = 1,
    combination;
 
 
   
do {
 printf("\nEnter the number of items in the list (n): ");
 scanf("%d", &userInput1);
       if ( userInput1 < 1 || userInput1 > 10 ) /* is n between 1 - 10 */
          printf("\n?Invalid input: Number must be between 1 and 10\n");
       else  
          break;
} while ( userInput1 < 1 || userInput1 > 10 );
 
 
 
  /* As long as n>=1 program will loop the following n*n-1 */
 
 int n; 
  for ( n = userInput1; n > 1; n--) {
      nFact = ( nFact * n );
  }    
 
  printf("n! = %d", nFact);
 
 
do {
 printf("\nEnter the number of items to choose (k): ");
 scanf("%d", &userInput2);    
       if ( userInput2 < 1 || userInput2 > 10 )  /* is k between 1 - 10 */
          printf("\n?Invalid input: Number must be between 1 and 10\n");
       else  
          break;
} while ( userInput2 < 1 || userInput2 > 10 );


int k;
  for ( k = userInput2; k > 1; k--) {
      kFact = ( kFact * k );
  }    

 
  printf("k! = %d", kFact);
printf("n = %d k = %d", userInput1, userInput2);

n_k = userInput1 - userInput2;

  while (n_k>=1)
  {
        nkFact = ( nkFact * n_k );
        n_k = ( n_k - 1 );
  }     

printf("\n(n-k)! = %d", nkFact);


combination = nFact / ( (nkFact) * kFact );


printf("\nNumber of combinations: %d", combination);




printf("\n");
system("PAUSE");
return(0);                                  
}

I got it to work, I went with a for statement instead. Its looks sloppy but I am going to clean it up a bit.

Better use double type to calculate factorials. Don't take the risk of integer overflow w/o any signals and with very strange results...

Your original code was fine. There was only one mistake and you corrected it in your second code.

If you look carefully at both, in the first you initialized n_k = (n-k)
but in the second you did the same thing but after you took the user input of n and k. This made your code work as you didn't have values for n and k when you ran your code until you manually entered them.

So if you had take n = 5 and k = 2, then it would have worked fine.

Also you should be careful that k cannot be greater than n as negative factorials don't exist.

You can do this by doing k<1 || k>n

You are 2 years late. Thread closed

commented: bah. i just got done writing an awesome post about calculating factorials, thinking this was a current thread. :( +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.