C Program to print the sum of digits till single digit.

Sum of Digits Till Single Digit :

SumOfDigits.c
#include<stdio.h> 
int main(void) 
{ 
    long num; 
    int dig,sum; 
    printf("Enter any number : "); 
    scanf("%ld",&num); 
    printf("%ld-> ",num); 
    do 
    { 
        sum = 0;  
        while(num!=0)  
        { 
            dig=num%10; 
            sum+=dig; 
            num/=10; 
        } 
        printf("%d-> ",sum); 
        num=sum; 
    }while(num/10!=0);     
    return 0; 
}

Output:

Terminal
Enter any number : 586596
586596-> 39-> 12-> 3

Happy Learning 🙂

About the Author:

Avatar
Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment