Integer – Sum of Digits

                      Integer - Sum of Digits


The program must accept an integer value N as a command line argument and print the sum of the digits in N.
Example Input/Output 1:
Input:
14
Output:
5
Example Input/Output 2:
Input:
1932
Output:
15

Code:

#include<stdio.h>
int main(int argc,char *argv[])
{
    int n=atoi(argv[1]);
    int r,sum=0;
    while(n!=0)
    {
        r=n%10;
        sum+=r;
        n=n/10;
    }
    printf("%d",sum);
}

Please do comment If u have any Queries!

Add a Comment

Your email address will not be published. Required fields are marked *