Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<stdio.h>
int dob(int num);
main()
{
        int num,c;
        printf("enter the number ");
        scanf("%d",&num);
        printf("the sum of the digits is %d",dob(num));
        return 0;
}
int dob(int num)
{
        int temp=0,c;
        if(num>10)
        {
                temp=num%10;
                num=num/10;
                return temp+(dob(num));
        }
}
Posted
Updated 21-Nov-14 1:58am
v4
Comments
den2k88 21-Nov-14 8:00am    
I'm sorry but can you tell us something more? The question is poorly worded - what do you mean with "How Do I Get The O/P In Single Digit"?

1 solution

Are you trying to calculate the sum of the digits in num recursively?
If that is the case, something like this might work for you;

C
#include<stdio.h>

int dob(int num);
int calc(int num);

int main() {
    int num, c;
    printf("enter the number ");
    scanf("%d", &num);
    printf("the sum of the digits is %d", calc(num));
    return 0;
}

int calc(int num) {
    int sum = num;
    do {
        sum = dob(sum);
    } while (sum > 9);
    return sum;
}

int dob(int num) {
    if (num < 10)
        return num;
    else
        return dob(num / 10) + dob(num % 10);
}



Hope this helps,
Fredrik
 
Share this answer
 
v2
Comments
naralasaimanoj 22-Nov-14 6:31am    
if i give i/p as 666 ,it will give 6+6+6=18,but as i want to get it in single digit (1+8 =9 ),
Fredrik Bornander 24-Nov-14 5:48am    
Added something that will calculate that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900