The program must accept an integer N as the input. The task is to count and print the number of digits that are both prime and even in the given number. If no such digits are found, print -1.
Note: Only 2 is both prime and even.
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 the digits that are both prime and even or -1.
Example Input/Output 1:
Input:
12345
Output:
1
Explanation:
The only digit that is both prime and even in 12345 is 2. Hence the count is 1.
Example Input/Output 2:
Input:
34567
Output:
-1
Explanation:
There are no digits that are both prime and even in 34567.
n = input().strip()
count = n.count('2')
print(count if count > 0 else -1)
#include<stdio.h>
int main() {
char n[12];
scanf("%s", n);
int count = 0;
for(int i = 0; n[i]; i++) {
if(n[i] == '2') 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(char c : n) {
if(c == '2') count++;
}
cout << (count > 0 ? count : -1);
return 0;
}
import java.util.Scanner;
public class CountPrimeEvenDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String n = sc.next();
int count = 0;
for(char c : n.toCharArray()) {
if(c == '2') count++;
}
System.out.println(count > 0 ? count : -1);
}
}
Leave a Reply