Sum of Digits in C Program

In this c program, we will learn how to code the sum of digits of a number by the help of (%) modulus operator.

Program of sum of digits –

#include <stdio.h>

int main()
{

int n, t, sum = 0, remainder;

printf("Enter an integer\n");

scanf("%d", &n);

t = n;

while (t != 0)
{

remainder = t % 10;

sum = sum + remainder;

t = t / 10;
}

printf("Sum of digits of %d = %d\n", n, sum);

 return 0;
}

Output –

Enter an integers

12345

Sum of digits of 12345 = 15

Post a Comment

0 Comments