Nice Programme to find BINOMIAL COEFFICIENT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//programme to find BINOMIAL COEFFICIENT - formular - n!/k!(n-k)!

#include<iostream>
#include<cstdlib>
using namespace std;
bool validateInput(int n, int k); //prototype the functions
int getfactorial(int n); //prototype the functions
int main()
{	
	int n,k,y;
	cout<<"Enter \'N\' and \'K\' "<<endl;
	cin>>n>>k;

	if(validateInput(n,k)==1) //valid the values that user has input
	{       
                //calling fuctions and apply the formular
		y = getfactorial(n)/(getfactorial(k)*getfactorial(n-k));	 

		cout<<"ANSWER IS : "<<y<<endl;				

	
	}
	else
	{
		cout<<"INVALID INPUTS, PLEASE RE-START THE PROGRAMME!"<<endl;
		system("PAUSE");
		return -1;
	}
system("PAUSE");
return 0;
}

bool validateInput(int n, int k) //functions to check Inputs
{
	bool result;
	if((n>0)&&(k>0)&&(k<n))
	{
		result = 1;
	}
	else
	{
		result = 0;
	}
return result;
}

int getfactorial(int n)//Function to get FACTORIALS
{
	int f=1;
	for(int i = 1;  i<=n;  )
	{
		f=f*i;
		i++;
		
	}
return f;
}


This is my 1st post. i am still learning c++, i am very happy to if you can receive some good programming practices.

AFTER DEBUGGED!
Last edited on
Test your code. Your factorial function is incorrect.
hmm.. ok, thanks

here its done now.. i correct some of my coding mistakes... i only test one value previously, that why i am not caught this i think :-D! thanks for the mentioning!
Last edited on
Topic archived. No new replies allowed.