Recursion
Factorial
GCD
#include<stdio.h>
unsigned long fact(int );
unsigned long fact(int );
void main()
{
int a;
printf("\nenter value of n ");
scanf("%d",&a);
printf("\n%d",fact(a));
/*b=sum(a);
printf("\n%d",b);*/
}
{
int a;
printf("\nenter value of n ");
scanf("%d",&a);
printf("\n%d",fact(a));
/*b=sum(a);
printf("\n%d",b);*/
}
unsigned long fact(int a)
{
if(a==1)
return 1;
return(a*fact(a-1));
}
{
if(a==1)
return 1;
return(a*fact(a-1));
}
Fibonacci
#include<stdio.h>
void main()
{
int a=0,i=0;//b=1,n,i=2,sum=0;
printf("\nenter length");
scanf("%d",&a);
for(i=1;i<=a;i++)
printf("\n%d",fib(i));
}
int fib(int a)
{
if(a==1)
return 0;
else if(a==2)
return 1;
return (fib(a-1)+fib(a-2));
}
{
int a=0,i=0;//b=1,n,i=2,sum=0;
printf("\nenter length");
scanf("%d",&a);
for(i=1;i<=a;i++)
printf("\n%d",fib(i));
}
int fib(int a)
{
if(a==1)
return 0;
else if(a==2)
return 1;
return (fib(a-1)+fib(a-2));
}
GCD
//greatest common devider
#include<stdio.h>
#include<stdio.h>
void main()
{
int a,b;
printf("\nenter values ");
scanf("%d%d",&a,&b);
printf("%d",gcd(a,b));
}
{
int a,b;
printf("\nenter values ");
scanf("%d%d",&a,&b);
printf("%d",gcd(a,b));
}
int gcd(int a,int b)
{
if(a==b)
return a;
else if(a%b==0)
return b;
else if(b%a==0)
return a;
else if(a>b)
return gcd(a%b,b);
else
return gcd(a,b%a);
}
{
if(a==b)
return a;
else if(a%b==0)
return b;
else if(b%a==0)
return a;
else if(a>b)
return gcd(a%b,b);
else
return gcd(a,b%a);
}
Comments
Post a Comment