Write a C Program to find the sum of digits of any number

Admin

 

Write a C Program to find the sum of digits of any number



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


Output



Enter a number : 98
Sum of digits of 98 is 17

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.