# Thread: [C] How to shorten this program?

1. ## [C] How to shorten this program?

Hi!

I'd like you to take a look at this code and tell me what should i do to make this shorter. This program count binomial coefficient. Thanks for all answers.
Code:
```#include "stdio.h"

int main()
{
int k,n,i,w;
while (scanf("%d%d",&n,&k)==2)
{
if(k>n/2)
k=n-k;
for(w=i=1;i<=k;i++)
w=w*(n++-i)/i;
printf("%d\n",w);
}
}```
I think that
Code:
`w=w*(n++-i)/i;`
might be write shorter, but i can't figure it out.

P.s. Sorry for my English...

2. Swap your if for this:
Code:
`k=k>n/2?n-k:k;`

Quzah.

3. Ok, thanks, is there anything else to do?

4. Code:
```    while (scanf("%d%d",&n,&k)==2)
{```
goes to

Code:
`    while (scanf("%d%d",&n,&k)==2) {`
why do you want to make it shorter?

it already looks cryptic with its 1 letter variable names and complete lack of spacing.

I compiled it and I dont think it works

5C3 = 5!/3!2! = 5*4/2 = 10

what algorithm are you using to calculate the coefficient?

5. w=w*(n++-i)/i;

can go to

w*=(n++-i)/i;

6. Code:
```#include "stdio.h"

int main()
{
int k,n,i,w;
while (scanf("%d%d",&n,&k)==2) for(k=k>n/2?n-k:k,w=i=1;i<=k;w*=(n++-i)/i,printf("%d\n",w),i++) ;
}```

7. Here's my final version.
Code:
```#include <stdio.h>
int main()
{
int k,n,i,w;
while (scanf("%d%d",&n,&k)==2) for(k=k>n/2?n-k:k,w=i=1;i<=k;w*=(n++-i)/i++,printf("%d\n",w)) ;
return 0;
}```

8. Code:
```#include <stdio.h>
int main(){int k,n,i,w;while(scanf("%d%d",&n,&k)==2)for(k=k>n/2?n-k:k,w=i=1;i<=k;i++,w*=(n++-i)/i,printf("%d\n",w));return 0;}```

9. Thanks @Dino, but it change my program, and doesn't work properly.

10. But what is the purpose of making the program cryptic and hard to read?

11. I have to do this program using as less as i can chars.

12. Originally Posted by milky
Thanks @Dino, but it change my program, and doesn't work properly.
Then it's worthless. (Did you run the version before or after my edit? Before the edit, I was incrementing i in the wrong spot.)

13. I run your latest version and it also change my program:/ Maybe there is another, shorter algorithm to count binomial coefficient?

14. It might be good to start with the proper formula in the first place: Binomial coefficient - Wikipedia, the free encyclopedia