Write a c program to print sum of digits.

Ram Pothuraju

 #include <stdio.h>


int main()

{

    int n, sum = 0, remainder;


    printf("Enter a positive integer: ");

    scanf("%d", &n);


    // Computing the sum of the digits

    while (n != 0)

    {

        remainder = n % 10;

        sum += remainder;

        n /= 10;

    }


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


    return 0;

}


In this program, we first take a positive integer as input from the user. Then, we loop through the digits of the number and add each digit to a variable sum. Finally, we print the computed value of sum.
Tags

Post a Comment

0Comments

Post a Comment (0)