0% found this document useful (0 votes)
10 views11 pages

REcursion

Uploaded by

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

REcursion

Uploaded by

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

Recursion

A function that calls itself is known as a recursive function.


And, this technique is known as recursion.

How recursion works?

void recurse()

... .. ...

recurse();

... .. ...

int main()

... .. ...

recurse();

... .. ...
}

#include <stdio.h>
int sum(int n);

int main() {
int number, result;

printf("Enter a positive integer: ");


scanf("%d", &number);
result = sum(number);

printf("sum = %d", result);


return 0;
}

int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}

Output

Enter a positive integer:3


sum = 6
Factorial of a Number Using Recursion
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Output

Enter a positive integer: 6


Factorial of 6 = 720

Program to calculate power using


recursion
#include <stdio.h>
int power(int n1, int n2);
int main() {
int base, a, result;
printf("Enter base number: ");
scanf("%d", &base);
printf("Enter power number(positive integer): ");
scanf("%d", &a);
result = power(base, a);
printf("%d^%d = %d", base, a, result);
return 0;
}

int power(int base, int a) {


if (a != 0)
return (base * power(base, a - 1));
else
return 1;
}
Run Code

Output

Enter base number: 3


Enter power number(positive integer): 4
3^4 = 81

Reverse a sentence using recursion


#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}

void reverseSentence() {
char c;
scanf("%c", &c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
}

Output

Enter a sentence: margorp emosewa


awesome program

Program to Convert Binary to Octal

#include <math.h>

#include <stdio.h>

int convert(long long bin);

int main() {

long long bin;

printf("Enter a binary number: ");

scanf("%lld", &bin);

printf("%lld in binary = %d in octal", bin, convert(bin));

return 0;

int convert(long long bin) {

int oct = 0, dec = 0, i = 0;


// converting binary to decimal

while (bin != 0) {

dec += (bin % 10) * pow(2, i);

++i;

bin /= 10;

i = 1;

// converting to decimal to octal

while (dec != 0) {

oct += (dec % 8) * i;

dec /= 8;

i *= 10;

return oct;

Enter a binary number: 101001


101001 in binary = 51 in octal

Program to Convert Octal to Binary


In this program, an octal number is converted to decimal at
first. Then, the decimal number is converted to binary number.

#include <math.h>
#include <stdio.h>
long long convert(int oct);
int main() {
int oct;
printf("Enter an octal number: ");
scanf("%d", &oct);
printf("%d in octal = %lld in binary", oct,
convert(oct));
return 0;
}

long long convert(int oct) {


int dec = 0, i = 0;
long long bin = 0;

// converting octal to decimal


while (oct != 0) {
dec += (oct % 10) * pow(8, i);
++i;
oct /= 10;
}
i = 1;

// converting decimal to binary


while (dec != 0) {
bin += (dec % 2) * i;
dec /= 2;
i *= 10;
}
return bin;
}
Run Code

Output
Enter an octal number: 67
67 in octal = 110111 in binary

GCD of Two Numbers using Recursion


#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1,
n2));
return 0;
}

int hcf(int n1, int n2) {


if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
Run Code

Output

Enter two positive integers: 366


60
G.C.D of 366 and 60 is 6.

Sum of Natural Numbers Using Recursion


#include <stdio.h>

int addNumbers(int n);

int main() {

int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}

int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}
Run Code

Output

Enter a positive integer: 20


Sum = 210

You might also like