Count of Even Digits in Odd Positions

The program must accept an integer N as the input. The task is to count and print the number of even digits in N that are present in odd positions. If no such digits are found, print -1.

Note: The position starts from 1 from the rightmost side.

Boundary Condition(s):
1 <= N <= 10^9

Input Format:
The first line contains the integer N.

Output Format:
The first line contains either the count of even digits that are present in odd positions or -1.

Example Input/Output 1:
Input:
456783

Output:
2

Explanation:
The even digits in odd positions are 8 (3rd position) and 4 (5th position). Hence the count is 2.

Example Input/Output 2:
Input:
13579

Output:
-1

Explanation:
There are no even digits in odd positions.

n = input().strip()
count = sum(1 for idx, digit in enumerate(reversed(n), 1) if int(digit) % 2 == 0 and idx % 2 == 1)
print(count if count > 0 else -1)
#include<stdio.h>

int main() {
    char n[12];
    scanf("%s", n);
    int count = 0, len = 0;
    while(n[len]) len++;
    for(int i = 0; i < len; i++) {
        if((len - i) % 2 == 1 && (n[i] - '0') % 2 == 0) count++;
    }
    printf("%d", (count > 0 ? count : -1));
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string n;
    cin >> n;
    int count = 0;
    for(int i = 0; i < n.size(); i++) {
        if((n.size() - i) % 2 == 1 && (n[i] - '0') % 2 == 0) count++;
    }
    cout << (count > 0 ? count : -1);
    return 0;
}
import java.util.Scanner;

public class CountEvenDigitsOddPositions {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int count = 0;
        for(int i = 0; i < n.length(); i++) {
            if((n.length() - i) % 2 == 1 && (n.charAt(i) - '0') % 2 == 0) count++;
        }
        System.out.println(count > 0 ? count : -1);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.