Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Friday 6 September 2013

Simple Example and Explanation of Recursion in C Programming

Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to "repeat the process". This makes it sound very similar to a loop because it repeats the same code, and in some ways it is similar to looping. On the other hand, recursion makes it easier to express ideas in which the result of the recursive call is necessary to complete the task. Of course, it must be possible for the "process" to sometimes be completed without the recursive call. One simple example is the idea of building a wall that is ten feet high; if I want to build a ten foot high wall, then I will first build a 9 foot high wall, and then add an extra foot of bricks. Conceptually, this is like saying the "build wall" function takes a height and if that height is greater than one, first calls itself to build a lower wall, and then adds one a foot of bricks. 


Program

#include<stdio.h>
#include<conio.h>

int sum(int n)
 {
  if(n==0)
   return n;
   else
    return n%10+sum(n/10);
  }
 void main()
  {
    int num;
    clrscr();
    printf("Enter the any digit of integer number");
    scanf("%d",&num);
    printf("sum of digit =%d",sum(num));
    getch();
  }
Share it Please

Pankaj Gaikar

Pankaj Gaikar is a professional blogger from Pune, India who writes on Technology, Android, Gadgets, social media and latest tech updates at Punk Tech , Being Android & Shake The Tech . Email me HERE

0 comments :

Post a Comment

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine