Tuesday, 9 December 2014

Program to find sum of digits of a number
#include<stdio.h>
#include<string.h>
/////////////program to print sum of digits///////////
void sumdigit(int n)
{
int sum=0,rem=0;
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("sum of digits is %d\n",sum);
sum);
}


main(int argc,char **argv)
{
int n,sum=0,rem=0,i,result=0;
for(i=1;i<argc;i++)
{
result=result+sum;
n=atoi(argv[i]);
sumdigit(n);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 123
sum of digits is 6
madan@madan-Lenovo-G570:~/madan$ ./a.out 14356
sum of digits is 19