logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Compute Combinations using Factorials

Posted on January 1, 2014 by Manish

This is a C++ to compute combinations using factorials.

Problem Description

1. This algorithm prints a total number of combination possible for given n and r value.
2. The time complexity of this algorithm is O(n).

Problem Solution

1. This algorithm takes the input of n and r value.
2. Print the result using ( n! / (r! * (n-r)!)).
3. Exit.

advertisement
Program/Source Code

C++ Program to compute combinations using factorials.
This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows system.

#include<iostream>
 
using namespace std;
 
// A function to find the factorial.
int factorial(int n)
{
	int i;
	for(i = n-1; i > 1; i--)
		n *= i;
 
	return n;
}
 
int main()
{
	int n, r,  result;
	cout<<"A program to find combination from nCr format using ( n! / (r! * (n-r)!))";
	cout<<"\n\n\tEnter the value of n: ";
	cin>>n;
	cout<<"\tEnter the value of r: ";
	cin>>r;
 
	// Get result using formula to calculate combinations.
	result = factorial(n)/(factorial(r)*factorial(n-r));
 
	cout<<"\nThe number of possible combinations is: "<<result;
}
Program Explanation

1. Take the input of n and r.
2. Using factorial(), calculate values for n!, r! and (n-r)!.
3. Using ( n! / (r! * (n-r)!)), print the result.
4. Exit.

advertisement
Runtime Test Cases
Case 1:
A program to find combination from nCr format using ( n! / (r! * (n-r)!))
 
        Enter the value of n: 5
        Enter the value of r: 3
 
The number of possible combinations is: 10
 
Case 2:
A program to find combination from nCr format using ( n! / (r! * (n-r)!))
 
        Enter the value of n: 13
        Enter the value of r: 4
 
The number of possible combinations is: 221

Sanfoundry Global Education & Learning Series – C++ Algorithms.
To practice all C++ Algorithms, here is complete set of 1000 C++ Algorithms.

« Prev Page - C++ Program to Compute Combinations using Matrix Multiplication
» Next Page - C++ Program to Describe the Representation of Graph using Adjacency Matrix

« C++ Program to Find All_Pairs_Shortest_Path
C++ Program to Implement Unordered_set in STL »

Deep Dive @ Sanfoundry:

  1. C Programming Examples on Computational Geometry Problems & Algorithms
  2. Java Programming Examples on Computational Geometry Problems & Algorithms
  3. C Programming Examples using Recursion
  4. C Programming Examples on Combinatorial Problems & Algorithms
  5. C Programming Examples on Numerical Problems & Algorithms
  6. Java Programming Examples on Numerical Problems & Algorithms
  7. Python Programming Examples
  8. C Programming Examples on Mathematical Functions
  9. C++ Programming Examples on Combinatorial Problems & Algorithms
  10. C++ Programming Examples on Numerical Problems & Algorithms
Manish Bhojasia
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.