C Program to find Sum of Digits of a given number

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

Comments

Popular posts from this blog

C Program to convert decimal to binary and find consecutive 1s in a binary number: