Computer Science, asked by yagna123, 1 year ago

write a program to accept a number and print the sum of its digits using while loop
input:123
output:6

Answers

Answered by kvnmurty
1
In C language:

#include <stdio.h>

main()
{
int  N, sum = 0;
     printf("a positive or negative Integer number: ");
     scanf("%d", &N);   
     if (N < 0)  N = - N ;
     while (N > 0) {
           sum += N % 10;   N /= 10;
     }
     printf("sum=%d", sum);
}

Similar questions