0% found this document useful (0 votes)
5 views2 pages

Recursive Functions

The document provides several recursive methods for various calculations in programming. These include methods to calculate the HCF and LCM of two numbers, convert decimal numbers to binary and octal, and count the number of 1's and digits in a number. Each method is defined with its respective logic and base cases for recursion.

Uploaded by

kunaltyagi2100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Recursive Functions

The document provides several recursive methods for various calculations in programming. These include methods to calculate the HCF and LCM of two numbers, convert decimal numbers to binary and octal, and count the number of 1's and digits in a number. Each method is defined with its respective logic and base cases for recursion.

Uploaded by

kunaltyagi2100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

[Write in your Computer Notebook.

]
Write recursive Methods.
1. To calculate HCF two numbers
int task(int m, int n)
{
if(m==n)
return m;
else if(m>n)
return task(m-n, n);
else
return task(m, n-m);
}
2. To convert a decimal number into binary
int binaryConvert(int n)
{
if(n==0)
return 0;
else
return binaryConvert(n/2) *10+(n%2);
}
3. To convert a decimal number into Octal
int octalConvert(int n)
{
if(n==0)
return 0;
else
return octalConvert(n/8) *10+(n%8);
}

4. To calculate LCM of two numbers without using HCF


int calculate(int a, int b, int i)//i=1
{
if ((i%a==0&&i%b==0))
return i;
else
return calculate(a, b, i+1);
}
}

5. To count and return the number of 1’s in ‘k’ using recursive technique
int countOne(int k)
{
if(k==0)
return 0;
else if(k%10==1)
return 1+ countOne(k/10); //each recursion adds 1
}
6. To count and return the number of digits in ‘n’ using recursive technique
int countDigit(int n)
{
if(n==0)
return 0;
else
return 1+ countDigit(n/10); //each recursion adds 1

You might also like