Given a number the task is to find the difference between sum of even and odd digits of the number.
Example 1: Let us take a number 3471. Here sum of even digits is 4 and sum of odd digits is 11. Hence their difference is 11-4 = 7.
Example 2: Let us take a number 8316. Here sum of even digits is 14 and sum of odd digits is 4. Hence their difference is 14-4 = 10.
C
#include <stdio.h>
int main()
{
int n=0,r=0,eSum=0,oSum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(n>0)
{
r=n%10;
if(r%2==0)
{
eSum=eSum+r;
}
else
{
oSum=oSum+r;
}
n=n/10;
}
if(eSum>oSum)
{
printf("The difference between the sum of odd and even number is %d",(eSum-oSum));
}
else
{
printf("The difference between the sum of odd and even number is %d",(oSum-eSum));
}
return 0;
}C