Sunday, 31 December 2017

C program to find out sum of digits of number using recursion

#include<stdio.h>
void rec(int n, int cal);
int main( )
{
     int n,cal=0;
    
     printf("Enter the number");
     scanf("%d",&n);

    rec(n,cal);


    return 0;

}

void rec(int n, int cal)

{
     int i;
     
     if(n==0)
       printf("sum of digits is %d",cal);

     if(n>0)

       {
         i=n%10;
         n=n/10;
         cal=cal+i;
       
         rec(n,cal);
       }
}






No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...