TCS NQT Set 2 | Day 1 2020 | odd and even difference

TCS NQT 2020 | Day 1 | Set 2
Given a maximum of 100 digit numbers as input, find the difference between the sum of odd
and even position digits.
Logic:
- Get the input as a string because we need up to 100 digits for eg 4567.
- Store added value of the odd position in variable a and even position in variable b
- Then convert the char value to integer value using ASCII value.
- Check the odd position the value is 4 added into variable a=4
- Check the next position which is the even position the value is 5 added into variable b=5
- Check the next position which is the odd position the value is 6 added into variable a=10
- Check the next position which is the even position the value is 7 added into variable b=12
- By using the abs() function we can find the difference of a and b to eliminate the negative value
- And then print the difference value a-b is 2
TCS NQT Set 2:
Program:
[code lang=”c”]
#include<stdio.h>
void 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 vise versa
{
b+=num[n-1]-48;
n–;
i=0;
}
}
printf("%d",abs(a-b)); //print the difference of odd and even
}
[/code]