Tuesday, March 31, 2009

Calculate sum of digits of five digit +ve number

/*Calculate sum of digits of 5 digit +ve number input through keyboard.The division operator & modulus can be used */

#include <stdio.h>
int main(void)
{
int num,temp = 0 ,a;
printf("Enter a +ve 5 digit number : ");
scanf("%d",&num);
if (( num <= 99999) && ( num > 0 ))
{
printf("The number you entered is %d ",num);
a = (num / 10000);
temp = a;
a = (num % 10000)/1000;
temp = temp + a;
a = ((num % 10000) % 1000) / 100 ;
temp = temp + a;
a = (((num % 10000) % 1000) % 100) / 10 ;
temp = temp + a;
a = (((num % 10000) % 1000) % 100) % 10;
temp = temp + a;
printf("\nSum of all five digits is %d",temp);
}
else
printf("\nOops not valid figure pl.try again");

return 0;
}

No comments: