Wednesday, October 4, 2017

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

Solution:
#include <stdio.h>
#include <stdlib.h>
void main(int c,char *n[])
{
    int a=atoi(n[1]),s=0;
    while(a)
    {
        s+=a%10;
        a/=10;
    }
    printf("%d",s);
}
Share: