13. Recursive Function
-----------------------------------------------------
(1)
#include<stdio.h>
int rec_fun_sum(int);
void main()
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
printf("Sum : %d\n",rec_fun_sum(num));
}
int rec_fun_sum(int num)
{
static int add=0;
if(num)
{
if(num%2==0)
{
add=add+(num%10);
rec_fun_sum(num/10);
}
else
rec_fun_sum(num/10);
}
return add;
}
-----------------------------------------------------
(2)
#include<stdio.h>
int rec_fun_count(int);
void main()
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
printf("Count : %d\n",rec_fun_count(num));
}
int rec_fun_count(int num)
{
static int c=0;
if(num)
{
if(num%10<6)
{
c++;
rec_fun_count(num/10);
}
else
rec_fun_count(num/10);
}
return c;
}
-----------------------------------------------------
(3)
#include<stdio.h>
int rec_fun_product(int);
void main()
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
printf("Product : %d\n",rec_fun_product(num));
}
int rec_fun_product(int num)
{
static int mul=1;
if(num)
{
if((num%10)%3==0)
{
mul=mul*(num%10);
rec_fun_product(num/10);
}
else
rec_fun_product(num/10);
}
else
return mul;
}
-----------------------------------------------------
(4)
#include<stdio.h>
int rec_fun_sum(int,int);
void main()
{
int num,c;
printf("Enter a number : ");
scanf("%d",&num);
printf("Enter how many last digits to add : ");
scanf("%d",&c);
printf("Sum : %d\n",rec_fun_sum(num,c));
}
int rec_fun_sum(int num,int c)
{
static int cu = 0,add=0;
if(cu<c)
{
cu++;
add = add + (num%10);
rec_fun_sum(num/10,c);
}
return add;
}
-----------------------------------------------------
(5)
#include<stdio.h>
int rec_fun_rev(int);
void main()
{
int num,c;
printf("Enter a number : ");
scanf("%d",&num);
printf("Rev : %d\n",rec_fun_rev(num));
}
int rec_fun_rev(int num)
{
static int num1=0;
if(num)
{
num1=num1*10+(num%10);
rec_fun_rev(num/10);
}
else
return num1;
}
-----------------------------------------------------
(6)
#include<stdio.h>
int rec_fun_perfect(int);
void main()
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
rec_fun_perfect(num)?printf("Perfect\n"):printf("Not Perfect\n");
}
int rec_fun_perfect(int num)
{
static int i=1,add=0;
if(i!=num)
{
if(num%i==0)
{
add=add+i;
i++;
rec_fun_perfect(num);
}
else
{
i++;
rec_fun_perfect(num);
}
}
if(add==num)
return 1;
else
return 0;
}
-----------------------------------------------------
(7)
#include<stdio.h>
int rec_fun_count(int *);
void main()
{
unsigned int num;
printf("Enter a number : ");
scanf("%d",&num);
printf("Set Bit : %d",rec_fun_count(&num));
}
int rec_fun_count(int *p)
{
static int c=0;
if(*p)
{
if(*p&1)
{
c++;
*p=*p>>1;
rec_fun_count(p);
}
else
*p=*p>>1;
rec_fun_count(p);
}
else
return c;
}
-----------------------------------------------------
(8)
#include<stdio.h>
int rec_fun_prime(int);
void main()
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
rec_fun_prime(num)?printf("Prime\n"):printf("Not Prime\n");
}
int rec_fun_prime(int num)
{
static int i=2;
if(i<num)
{
if(num%i==0)
return 0;
else
{
i++;
rec_fun_prime(num);
}
}
else if(i==num)
return 1;
}
-----------------------------------------------------
(9)
#include<stdio.h>
int rec_fun_count_arr(int *,int);
void main()
{
int a[6]={71,53,145,21,49,153};
int ele;
ele=sizeof(a)/sizeof(a[0]);
printf("Count = %d ",rec_fun_count_arr(a,ele));
}
int rec_fun_count_arr(int *p,int ele)
{
static int c = 1, q=0;
if(q<ele)
{
if(*p>=38 && *p<=99)
{
c++;
q++;
rec_fun_count_arr(++p,ele);
}
else
{
q++;
rec_fun_count_arr(++p,ele);
}
}
else
return c;
}
-----------------------------------------------------
(10)
#include<stdio.h>
int rec_fun_sum_arr(int *,int);
void main()
{
int a[6]={10,20,30,44,55,66};
int ele;
ele=sizeof(a)/sizeof(a[0]);
printf("Sum = %d ",rec_fun_sum_arr(a,ele));
}
int rec_fun_sum_arr(int *p,int ele)
{
static int sum = 0, q=0;
if(q<(ele/2))
{
sum=sum + *p;
++q;
rec_fun_sum_arr(++p,ele);
}
else
return sum;
}
-----------------------------------------------------
(11)
#include<stdio.h>
void rec_fun_rev_arr(int *,int *);
void main()
{
int a[6]={11,22,33,44,55,66},ele,i;
ele=sizeof(a)/sizeof(a[0]);
rec_fun_rev_arr(a,a+(ele-1));
for(i=0;i<ele;i++)
printf("%d ",a[i]);
}
void rec_fun_rev_arr(int *p,int *q)
{
if(p<q)
{
*p=*p^*q;
*q=*p^*q;
*p=*p^*q;
rec_fun_rev_arr(++p,--q);
}
}
-----------------------------------------------------
(12)
#include<stdio.h>
void rec_fun_rev_string(char *,char *);
void main()
{
char s[20]="123 abc 789",len,i;
for(len=0;s[len];len++);
rec_fun_rev_string(s,s+(len-1));
printf("%s",s);
}
void rec_fun_rev_string(char *p,char *q)
{
if(p<q)
{
*p=*p^*q;
*q=*p^*q;
*p=*p^*q;
rec_fun_rev_string(++p,--q);
}
}
-----------------------------------------------------
(13)
#include<stdio.h>
int rec_fun_count_string(char *,char);
void main()
{
char s[20]="123 aacc tata",ch='a';
printf("Count = %d",rec_fun_count_string(s,ch));
}
int rec_fun_count_string(char *p,char ch)
{
static int c=0;
if(*p)
{
if(*p==ch)
{
c++;
rec_fun_count_string(++p,ch);
}
else
rec_fun_count_string(++p,ch);
}
else
return c;
}
-----------------------------------------------------
(14)
#include<stdio.h>
int rec_fun_rev_bit(int);
void rec_fun_binary(int);
void main()
{
int num,num1;
printf("Enter a number : ");
scanf("%d",&num);
rec_fun_binary(num);
num1=rec_fun_rev_bit(num);
rec_fun_binary(num1);
}
int rec_fun_rev_bit(int num)
{
static int p=0,q=31,i;
if(p<q)
{
int n1,n2;
n1=num>>p&1;
n2=num>>q&1;
if(n1!=n2)
{
num=num^(1<<p);
num=num^(1<<q);
}
p++;
q--;
rec_fun_rev_bit(num);
}
else
return num;
}
void rec_fun_binary(int num)
{
static int c=31;
if(c>=0)
{
printf("%d",(num>>c)&1);
c--;
rec_fun_binary(num);
}
else
{
printf("\n");
c=31;
}
}
-----------------------------------------------------
(15)
#include<stdio.h>
void my_strncpy(char *,char *,int );
void main()
{
char s1[20],s2[20];
int n;
printf("Enter string 1 : ");
scanf("%s",s1);
printf("Enter string 2 : ");
scanf("%s",s2);
printf("Enter how many characters to copy : ");
scanf("%d",&n);
my_strncpy(s1,s2,n);
printf("%s",s1);
}
void my_strncpy(char *p,char *q,int n)
{
static int c=0;
if(c<n)
{
p[c]=q[c];
c++;
my_strncpy(p,q,n);
}
}
-----------------------------------------------------
(16)
#include<stdio.h>
int my_strncmp(char *,char *,int );
void main()
{
char s1[20],s2[20];
int n;
printf("Enter string 1 : ");
scanf("%s",s1);
printf("Enter string 2 : ");
scanf("%s",s2);
printf("Enter how many characters to compare : ");
scanf("%d",&n);
printf("|%d|",my_strncmp(s1,s2,n));
}
int my_strncmp(char *p,char *q,int n)
{
static int c=0;
if(c<n)
{
if(p[c]==q[c])
{
c++;
my_strncmp(p+c,q+c,n);
}
else
return p[c]-q[c];
}
}
-----------------------------------------------------
(17)
#include<stdio.h>
char* my_strchr(char *,char );
void main()
{
char s[20],ch;
int n;
printf("Enter string : ");
scanf("%s",s);
printf("Enter the character you want to search : ");
scanf(" %c",&ch);
printf("%s",my_strchr(s,ch));
}
char* my_strchr(char *p,char ch)
{
if(*p)
{
if(*p==ch)
return p;
else
my_strchr(++p,ch);
}
else
return 0;
}
Comments
Post a Comment