Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Given maximum of 100 digit numbers as input, find difference between sum of odd and even position

Oddly Even:
Given a maximum of 100 digit numbers as input, find the difference between the sum of odd and even position digits.

Case 1:
Input: 4567
Expected Output: 2
Explantion : Odd positions are 4 and 6 as they are pos: 1 and pos: 3, both have sum 10. Similarly, 5 and 7 are at even positions pos: 2 and pos: 4 with sum 12. Thus, difference is 12 - 10 = 2
by

1 Answer

akshay1995

// Find the Nth term in the series
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
int a = 0,b = 0,i = 0, n;
char num[100];

printf("Enter the number:");
scanf("%s",num); //get the input up to 100 digit
n = strlen(num);
while(n>0)
{
if(i==0) //add even digits when no of digit is even and vise versa
{
a+=num[n-1]-48;
n--;
i=1;
}
else //add odd digits when no of digit is even and vice versa
{
b+=num[n-1]-48;
n--;
i=0;
}
}
printf("Difference between the sum of odd and even position digits is %d",abs(a-b)); //print the difference of odd and even

return 0;
}

Login / Signup to Answer the Question.