Program to find some of digits using recursion in C


C Program to find some of digits using recursion

#include <stdio.h>
#include <conio.h>
int sum_digits (int);
int sum = 0;
main ()
{
int n, a;a
clrscr ();
printf (“Enter a number  :”);
scanf (“%d”, &n);
a = sum_digits(n);
printf (“Sum of digits of %d = %d”, n,a);
getch ();
}
int sum_digits(int n)
{
if (n==0)
return (sum);
else
{
sum = sum +n%10;
sum_digits (n/10);     /*recursive call*/
return (sum);
}}
OUTPUT
Enter a number : 3521
Sum of digits of 3521 = 11
Explanation :
Here the value of sum is returned when the function is called with (n==0) else the function is called ach time with value (n/10). Here exit criteria is n == 0.

Some other important programs of C in recursion which every programmer should know.

·         C program to find HCF of n numbers using recursion.
#include <stdio.h>
#include <stdio.h>
int hcf (int, int);
void main ()
{
int n, i, j, h;
int a[10];
clrscr ();printf (“Enter how any numbers :”);
scanf (%d”, &n);
printf (“Enter %d numbers whose hcf you want to compute :”);
for (i=0; i<n; i++)
scanf (“%d”, &a[i] );
for (i=0; i<n-1; i++)
{
h = hcf(a[i], a[i+1]);
a [i+1] = h;
}
printf (“The hcf = %d”, h);
getch ();
}
int hcf (int a, int b)
{
if (a%b == 0)
return (b);
else
return (hfc (b, a%b));
}
OUTPUT
Enter hoe many numbers : 3
Enter 3 numbers whose hcf you want to compute : 11 22 33
the hcf = 11
Explanation :
Here first numbers whose hcf is to be determined are entered = 11 22 33 (say)
First pass, i =0, hcf (a[0], a[1]) = hcf (11, 22)) = 11   a[1] = -> a[1] = 11
Second pass, i = 1, h = hcf (a[1], a[2]) = hcf (11, 33) = 11
Third pass, i = 2, condition fails and value of h gets printed.
   In this program, hcf is calculated using recursive function, a number is divided by another and remainder is generated, If reminder is 0, then hcf is the divisor, but if reminder is not zero, the function is again called with reminder as the divisor as the dividend. In case of list of numbers whose hcf is to be calculated, first find hcf of the first 2 numbers, Then result is replaced at the position of 2nd number and function is calculated using the 2nd and 3rd number.
·         C program to print the Fibonacci number using recursion.
#include <stdio.h>
#include <conio.h>
int fib(int);
void main ()
{
int i, n;
clrscr ();
printf (“Enter number of Fibonacci terms to print :”);
scanf (“%d”, &n);
for (i=1; i<=n; i++)
printf (“%d\t”, fib(i) );
getch ();
}
int fib (int m)
{
if (m==1)
return (0);
if (m==2)
return (1);
else
return (fib(m-1)+ fib(m-2));
}
OUTPUT
Enter numbers of Fibonacci terms to print : 6
0     1     1     2     3     5
Explanation :
In this program, recursion is used because the Fibonacci number n is generated to sum of its number
 fib(m) = fib(m-1) + fib(m-2)
here fib() is a function which computes nth Fibonacci number. The exit criteria is that if m==1 then return 0 and if exit criteria is m==2 then return 1.
·         C program to reverse the string using recursion.
#include <stdio.h>
#include <conio.h>
void reverse (void)
void main ()
{
clrscr ();
printf (“Enter a line of text below\n”);
reverse ();
getch ();
}
void reverse (void)
{
char c;
if ((c= getchar() )!=’\n’)
reverse ();
putchar c;
return;
}
OUTPUT
Enter a line of text below
Learn programming in c
c ni gnimmargorp nrael


No comments

Powered by Blogger.