Pages

Simple C Program to Calculate Sum of Digits of a Number


This is a very simple C Language program that calculates the sum of digits of a 5 digit number. The 5 digit number is entered by the user. Here's the code and output of the program. Enjoy!!!

Code



#include <stdio.h>
int main()
{
    int num, d1, d2, d3, d4, d5,sum;
    printf("Enter a five digit number:");
    scanf("%d",&num);
    d1 = num / 10000;
    d2 = (num % 10000) / 1000;
    d3 = (num % 1000) / 100;
    d4 = (num % 100) / 10;
    d5 = num % 10;
    sum = d1 + d2 + d3 + d4 + d5;
    printf("sum: %d", sum);
    return 0;
}

Output



You can go through the code and in case of any confusion, you can feel free to ask me by using the comment form below.


Don't Forget to Share...