Sum of all the digits of a number using Command Line Language

Ques. Find the sum of the digits of a number by command line Arguments?

Write a program to find sum of digits of a number using command line arguments

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
    long num, temp, digit, sum = 0;
    if(argc == 1 ||  argc > 2)
    {
         printf("Enter the number\n");
         exit(1);
    }
    num = atoi (argv[1]) ;
    temp = num;
    while (num > 0)
    {
        digit = num % 10;
        sum  = sum + digit;
        num /= 10;
    }
    printf("Sum of the digits of %ld = %ld\n", temp, sum);
}

Check all other command Line Programs for TCS here on our Command line Programming Dashboard

One comment on “Sum of all the digits of a number using Command Line Language”