/* Towers of Hanoi by recursion */
#include<stdio.h>
void toh(int,char,char,char);
int main()
{
int n=3;
toh(n,'A','B','C');
return 0;
}
void toh(int n,char a,char b,char c)
{
if(n==1)
printf("\nMoved from %c to %c",a,c);
else
{
toh(n-1,a,c,b);
toh(1,a,' ',c);
toh(n-1,b,a,c);
}
}
A Tech publication for all. Get Knowledge about Web Development, Programming, and trending news, tutorials, and tools for beginners to experts.
Showing posts with label Recursion. Show all posts
Showing posts with label Recursion. Show all posts
Friday, September 23, 2016
Towers of Hanoi by recursion
Binary by recursion
/* Binary by recursion */
#include<stdio.h>
void binary(long);
int main()
{
long n;
printf("Type a value : ");
scanf("%ld",&n);
binary(n);
return 0;
}
void binary(long n)
{
if(n>1)
binary(n/2);
printf("%ld",n%2);
}
GCD by Recursion
/* greatest common divisor by recursion */
#include<stdio.h>
int gcd(int,int);
int main()
{
int a,b;
printf("Type 2 values to find GCD :\n");
scanf("%d %d",&a,&b);
printf("GCD : %d",gcd(a,b));
return 0;
}
int gcd(int m,int n)
{
if(n>m) return gcd(n,m);
if(n==0) return m;
return gcd(n,m%n);
}
Sum of digit by Recursion
/* Sum of digit by recursion */
#include<stdio.h>
int sod(int);
int main()
{
int i;
printf(" Type any value : ");
scanf("%d",&i);
printf("Sum of digit : %d",sod(i));
return 0;
}
int sod(int n)
{
if(n<1)
return 0;
return(n%10+sod(n/10));
}
Reverse by Recursion
/* Reverse by Recursion */
#include<stdio.h>
int rev(int,int);
int main()
{
int a;
printf("Type a value : ");
scanf("%d",&a);
printf("Reverse: %d",rev(a,0));
return 0;
}
int rev(int i,int r)
{
if(i > 0)
return rev(i/10,(r*10)+(i%10));
return r;
}
Fibonacci by Recursion
/* Fibonacci by Recursion */
#include<stdio.h>
int fib(int);
int main()
{
printf("Type any value : ");
printf("\nNth value: %d",fib(getche()-'0'));
return 0;
}
int fib(int n)
{
if(n<=1)
return n;
return(fib(n-1)+fib(n-2));
}
Power by Recursion
/* Power by Recursion */
#include<stdio.h>
int pow(int,int);
int main()
{
int i,j;
printf("Type two values : \n");
scanf("%d %d",&i,&j);
printf("i pow j = %d",pow(i,j));
return 0;
}
int pow(int i,int j)
{
if(j==1)
return i;
return (i*pow(i,j-1));
}
Factorial by Recursion
/* Factorial by Recursion */
#include<stdio.h>
int fact(int);
int main()
{
int n;
printf("Type any value : ");
scanf("%d",&n);
n=fact(n);
printf("\nFactorial : %d ",n);
return 0;
}
int fact(int x)
{
if(x==1)
return(x);
else
x=x*fact(x-1);
}
Subscribe to:
Posts (Atom)
B
BEING CODERS
Take Your Coding Journey Further!
Find more coding tutorials, technology insights, programming guides, and practical knowledge. Choose your reading experience below!
✦ Keep Learning. Keep Building. Keep Coding. ✦
-
1 23 456 78910 #include<stdio.h> int main() { int i,j,k; k=1; for(i=1;i<5;i++) { for(j=1;j<=i;j++) { printf("%d",k++);...
-
Learn how to configure Let’s Encrypt SSL on Windows 11 using Win-ACME and IIS. Securing your website with HTTPS is no longer optional—it’s a...
-
Complete beginner-friendly guide to generate and install a free SSL certificate on Windows server If you’re running a website on Windows Se...
-
A fresh year brings fresh possibilities. ✨ New goals to work toward. New memories to make. New ideas waiting to take shape. But with so m...
-
ABCDE BCDE CDE DE E #include <stdio.h> int main() { int i, j; for(i=1;i<=5;i++) { for(j=i;j<=5;j++) ...
-
World Country, State and City Database in SQL and JSON Format (Step-by-Step Guide) If you want to add all countries, states/provinces, and ...
-
How do you make a mark with a new company in a competitive market? How did Facebook reach its first $100 million mark in revenue? The answer...
-
🌍 World Geo Data — Full Country, State, City & Currency Dataset This repository provides an overview of the World Geo Data digital pro...
-
1234567 12345 123 1 int main() { int i,j; for(i=7;i>=1;i-=2) { for(j=1;j<=i;j++) { printf("%d",j); } printf("\n");...
-
1 10 101 1010 10101 #include<stdio.h> int main() { int i,j,k; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d",j%2)...