C Program to Calculate the Sum of Digits of a given Integer

C Programming Computer Science

In this program we are going to calculate the sum of digits in a given integer using C Program.

  • Source Code

/*
 * C program to accept an integer & find the sum of its digits
 */
#include <stdio.h>
 
void main()
{
    long num, temp, digit, sum = 0;
 
    printf("Enter the number \n");
    scanf("%ld", &num);
    temp = num;
    while (num > 0)
    {
        digit = num % 10;
        sum  = sum + digit;
        num /= 10;
    }
    printf("Given number = %ld\n", temp);
    printf("Sum of the digits %ld = %ld\n", temp, sum);
}

 

  • Runtime
Enter the number
202
Given number = 202
Sum of the digits 202 = 4
 
 
Enter the number
56789
Given number = 56789
Sum of the digits 56789 = 35

 

Also Read: 

C PROGRAM TO FIND THE NUMBER OF INTEGERS DIVISIBLE BY 3

C PROGRAM TO CHECK IF A GIVEN INTEGER IS POSITIVE OR NEGATIVE

C PROGRAM TO CALCULATE THE SUM OF ODD AND EVEN NUMBERS

C PROGRAM TO CHECK IF A GIVEN INTEGER IS ODD OR EVEN

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.