0% found this document useful (0 votes)
5 views

Programs-Recursion

The document contains Java programs for calculating permutations, combinations, checking if a number is special based on the sum of the factorials of its digits, and calculating the sum of a specific series. Each program includes methods for input, factorial calculation, and the main logic for the respective mathematical operation. The programs demonstrate recursive function usage and basic input/output in Java.

Uploaded by

Naman Crist Vats
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Programs-Recursion

The document contains Java programs for calculating permutations, combinations, checking if a number is special based on the sum of the factorials of its digits, and calculating the sum of a specific series. Each program includes methods for input, factorial calculation, and the main logic for the respective mathematical operation. The programs demonstrate recursive function usage and basic input/output in Java.

Uploaded by

Naman Crist Vats
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAMS

𝒏!
1. Calculating Permutation P(n,r)= (𝒏−𝒓)!

import java.util.*;
class Permutation
{
int r;
int n;
Permutation()
{
n=0;
r=0;
}
void input()//accepting values of n and r
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n:");
n=sc.nextInt();
System.out.println("Enter value of r:");
r=sc.nextInt();
}
int factorial(int x)//calculating factorial of x recursively
{
if(x==1)
return 1;
return (x*factorial(x-1));
}
void compute()
{
int a = factorial(n);//calculating n!
int b= factorial(n-r);//calculating(n-r)!
int p=a/b;
System.out.println("Answer:"+p);
}
public static void main(String args[])
{
Permutation obj= new Permutation();
obj.input();
obj.compute();
}
}
𝒏!
2. Calculating Combination P(n,r)=
𝒓!(𝒏−𝒓)!

import java.util.*;
class Combination
{
int r;
int n;
Combination()
{
n=0;
r=0;
}
void input()//accepting values of n and r
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n:");
n=sc.nextInt();
System.out.println("Enter value of r:");
r=sc.nextInt();
}
int factorial(int x)//calculating factorial of x recursively
{
if(x==1)
{
return 1;
}
return (x*factorial(x-1));
}
void compute()
{
int a = factorial(n);//calculating n!
int b = factorial(r);//calculating r!
int c= factorial(n-r);//calculating(n-r)!
int p=a/(b*c);
System.out.println("Answer:"+p);
}
public static void main(String args[])
{
Combination obj= new Combination();
obj.input();
obj.compute();
}
}
3. Sum of factorials of the digits of a number is equal to the number itself
(eg: 145= 1!+4!+5!)

import java.util.*;
class Special
{
int num;
int sum;
Special()
{
num=0;
sum=0;
}
void accept()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number:");
num=sc.nextInt();
}
int factorial(int x)// factorial of digit x
{

if(x==1)
return 1;
return (x*factorial(x-1));
}
void isSpecial(int n)
{
int r=0,n1=n;
sum=0;
while(n>0)
{
r=n%10;
sum=sum+factorial(r);
n=n/10;
}
if(sum==n1)
System.out.println(num+"is a special number.");
else
System.out.println(num+"is not a special number.");
}
void display()
{
accept();
isSpecial(num);

}
public static void main (String args[])
{
Special obj=new Special();
obj.display();
}
}

𝒙𝟐 𝒙𝟑 𝒙𝟒 𝒙𝒏
4. Calculating sum of the series S = x - + - + …………. +/- (
𝟐! 𝟑! 𝟒! 𝒏!
𝒙𝟏
= x)
𝟏!
import java.util.*;
class Series
{
int x;
int n;
double S;
Series()
{
x=0;
n=0;
S=0;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n:");
n=sc.nextInt();
System.out.println("Enter value of x:");
x=sc.nextInt();
}

int factorial(int n)
{
if(n==1)//base case
return 1;
return(n*factorial(n-1));
}
int power(int a, int b)
{
if(b==0)// base case
return 1;
return (a*power(a,(b-1)));
}
void calculate()
{
int p=0;
int f=0;
for(int i=1;i<=n;i++)
{
p=power(x,i);
f=factorial(i);
if(i%2==0)
S=S-((double)p/f);
else
S=S+((double)p/f);
}
}
void display()
{
accept();
calculate();
System.out.println("Sum of the series:"+S);
}
public static void main(String args[])
{
Series obj= new Series();
obj.display();
}
}

You might also like