Loading [MathJax]/extensions/tex2jax.js

C++ Program to Print nCr using For Loop

C++ Program to Print nCr using For Loop

Write C++ Program to Print nCr using For Loop

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// CPP Program to Print nCr using For Loop
#include <iostream>
using namespace std;
int fact(int n)
{
int f = 1, i;
for(i = 1; i <= n; i++)
{
f = f * i;
}
return f;
}
int main()
{
int n, r;
cout << "Enter the value of n:--> ";
cin >> n;
cout << "Enter the value of r :--> ";
cin >> r;
int result = fact(n)/(fact(r) * fact(n-r)); //combination formula
cout << "The value of nCr is " << result;
return 0;
}
// CPP Program to Print nCr using For Loop #include <iostream> using namespace std; int fact(int n) { int f = 1, i; for(i = 1; i <= n; i++) { f = f * i; } return f; } int main() { int n, r; cout << "Enter the value of n:--> "; cin >> n; cout << "Enter the value of r :--> "; cin >> r; int result = fact(n)/(fact(r) * fact(n-r)); //combination formula cout << "The value of nCr is " << result; return 0; }
// CPP Program to Print nCr using For Loop

#include <iostream>

using namespace std;

int fact(int n)
{
	int f = 1, i;
	for(i = 1; i <= n; i++)
        {
		f = f * i;
	}
	return f;
}

int main()
{
	int n, r;
	cout << "Enter the value of n:--> ";
	cin >> n;

	cout << "Enter the value of r :--> ";
	cin >> r;

	int result = fact(n)/(fact(r) * fact(n-r));  //combination formula

	cout << "The value of nCr is " << result;

	return 0;
}

Output:

Enter the value of n:--> 5
Enter the value of r :--> 3

The value of nCr is 10