Difference - Sum of Odd and Even Digits

Given a maximum of hundred digits as the input. The program must print the difference between the sum of odd and even digits as the output. If the input is not a valid number, then print Invalid as the output.
Example Input/Output 1:
Input:
118745913
Output:
15
Explanation:
The sum of odd digits is 27 (1, 1, 7, 5, 9, 1 and 3).
The sum of even digits is 12 (8 and 4).
So the difference is 27-12 = 15.
Hence the output is 15
Example Input/Output 2:
Input:
235468173645
Output:
-6
Example Input/Output 3:
Input:
76320Afk384
Output:
Invalid
Note: The invalid number may contain white spaces.

Solution :-

#include<stdio.h>

int main()
{
    char str[1000];
    scanf("%[^\n]s",str);
    
    int len=strlen(str),flag=0,even=0,odd=0;
    
    for(int i=0;i<len;i++)
    {
        if(isalpha(str[i]) || str[i]==' ' || isalnum(str[i])==0)
        {
            flag=1;
            break;
        }
        
        int a=str[i]-48;
        
        if(a%2==0)
        {
            even=even+a;
        }
        else
        odd=odd+a;
        
    }
    
    if(flag==1)
    {
        printf("Invalid");
    }
    else
    {
        printf("%d",odd-even);
    }
    
}

Comments

Popular posts from this blog

Two Matrix Spiral Print

Alphabets Positions Reversed

Odd Factors - (Error Identification) skillrack program id - 7306