Q:

Write a C program to print sum of digits enter by user

0

Write a C program to print sum of digits enter by user

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<stdio.h>

int main()
{
    int num, total;

    //Reading number
    printf("Enter any number: ");
    scanf("%d", &num);

    //Adding sum of digit in total variable
    for(total = 0; num > 0; num = num/10)
        total = total + (num%10);

    //Printing sum of digit
    printf("Sum of digits: %d", total);

    return 0;
}

Result:

Enter any number: 502

Sum of digits: 7

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now