Sum of digits of any number - C - A5P9



/* Write a program to find the sum of digits of any number */

#include<stdio.h>
#include<conio.h>
int sum_digit(int);
void main()
{
    int no,ans;
    clrscr();
    printf("Enter Number ->");
    scanf("%d",&no);
    ans=sum_digit(no);
    printf("\nSum of Digits -> %d",ans);
    getch();
}
int sum_digit(int n)
{
    int sum=0,rem;
    while(n!=0)
    {
        rem=n%10;
        sum=sum+rem;
        n=n/10;
    }
    return sum;
}

Post a Comment