C Programming

C Program – Print the sum of digits of given number

C Program to print the sum of digits of a given number.

Sum of Digits :

SumOfDigits.c
#include<stdio.h> 
int main(void) 
{ 
    int n,sum=0,rem; 
    printf("Enter a number to Calculate Sum: "); 
    scanf("%d",&n); 
    while(n>0) 
    { 
        rem = n%10;     
        sum+=rem; 
        n/=10;         
    } 
    printf("Sum of Digits=%d\n",sum); 
    return 0; 
}

Output:

Terminal
Enter a number to Calculate Sum: 56489
Sum of Digits=32

Happy Learning :)

 

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Ask a question or leave a comment