Program Discussion :: Basics
320 / 279
Write a program in C using command line argument to input one number and print Sum of digits of number.
Answer:
#include
#include
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);
}
Asked In ::
Language:
OM Prakesh
28 Oct, 2017 2:30 PM
#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);
}