Program to calculate the value of nCr

Program to calculate the value of nCr in c

#include<stdio.h>
#include<conio.h>
int Factorial(int fact);
void Calculate_nCr(int n,int r);
void main()
{
	int n,r;
	printf("Enter the value of n \n");
	scanf("%d",&n);
	printf("Enter the value of r \n");
	scanf("%d",&r);
	Calculate_nCr(n,r);
	getch();
}
void Calculate_nCr(int n,int r)
{
	int nCr=Factorial(n)/(Factorial(r)*(Factorial(n-r)));
	printf("nCr Result = %d",nCr);
}
int Factorial(int fact)
{
    int total = 1;
    for (int i = fact; i >= 1; i--)
    {
        total *= i;
    }
    return total;
}

Output

Enter the value of n
5
Enter the value of r
2
nCr Result = 10

Program to calculate the value of nCr in c++

#include<iostream>
#include<conio.h>
using namespace std;
class nCr
{
public:
	void Calculate_nCr(int n,int r)
	{
		int nCr=Factorial(n)/(Factorial(r)*(Factorial(n-r)));
		cout<<"nCr Result = "<<nCr;
	}
public:
	int Factorial(int fact)
	{
		int total = 1;
		for (int i = fact; i >= 1; i--)
		{
			total *= i;
		}
		return total;
	}
};
void main()
{
	int n,r;
	cout<<"Enter the value of n \n";
	cin>>n;
	cout<<"Enter the value of r \n";
	cin>>r;
	nCr ncr;
	ncr.Calculate_nCr(n,r);
	getch();
}

Output

Enter the value of n
5
Enter the value of r
3
nCr Result = 10

Program to calculate the value of nCr in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nCrProject
{
    class nCr
    {
	public void Calculate_nCr(int n,int r)
	{
		int nCr=Factorial(n)/(Factorial(r)*(Factorial(n-r)));
		Console.WriteLine("nCr Result = "+nCr);
	}
	public int Factorial(int fact)
	{
		int total = 1;
		for (int i = fact; i >= 1; i--)
		{
			total *= i;
		}
		return total;
	}
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the value of n");
            int n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the value of r");
            int r = Convert.ToInt32(Console.ReadLine());
            nCr ncr = new nCr();
            ncr.Calculate_nCr(n, r);
            Console.ReadLine();
        }
    }
}

Output

Enter the value of n
6
Enter the value of r
2
nCr Result = 15

Program to calculate the value of nCr in java

import java.util.*;
class nCr
{
	public void Calculate_nCr(int n,int r)
	{
		int nCr=Factorial(n)/(Factorial(r)*(Factorial(n-r)));
		System.out.println("nCr Result = "+nCr);
	}
	public int Factorial(int fact)
	{
		int total = 1;
		for (int i = fact; i >= 1; i--)
		{
			total *= i;
		}
		return total;
	}
}
class examples
{
public static void main(String[] args)
	{
	 nCr ncr=new nCr();
	 Scanner sc=new Scanner(System.in);
	 System.out.println("Enter the value of n");
	 int n=sc.nextInt();
	 System.out.println("Enter the value of r");
	 int r=sc.nextInt();
	 ncr.Calculate_nCr(n, r);
	}
}

Output

Enter the value of n
7
Enter the value of r
3
nCr Result = 35

Write a comment


Name Email Message