Thursday, 30 August 2012

If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the modulus operator ‘%’)


main()
{
 long num,a,b,c,d,e,sum;
 clrscr();
 printf("Enter a five digit number\n");
 scanf("%ld",&num);
 a=num%10; b=num/10;
 c=b%10; b=b/10;
 d=b%10; b=b/10;
 e=b%10; b=b/10;
 sum=a+c+d+e+b;
 printf("Sum of the digits of %ld is %ld",num,sum);
}

output:-

1 comment: