Count Digits That Are Fibonacci Numbers

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

Note: The Fibonacci numbers less than 10 are 0, 1, 2, 3, 5, and 8.

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 Fibonacci numbers or -1.

Example Input/Output 1:
Input:
12345

Output:
3

Explanation:
The digits that are Fibonacci numbers in 12345 are 1, 2, and 3. Hence the count is 3.

Example Input/Output 2:
Input:
4690

Output:
1

Explanation:
The only digit that is a Fibonacci number in 4690 is 0.

n = input().strip()
fib_digits = {'0', '1', '2', '3', '5', '8'}
count = sum(1 for digit in n if digit in fib_digits)
print(count if count > 0 else -1)
#include<stdio.h>
#include<stdbool.h>

int main() {
    char n[12];
    scanf("%s", n);
    int count = 0;
    for(int i = 0; n[i]; i++) {
        if(n[i] == '0' || n[i] == '1' || n[i] == '2' || n[i] == '3' || n[i] == '5' || n[i] == '8') 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 == '0' || c == '1' || c == '2' || c == '3' || c == '5' || c == '8') count++;
    }
    cout << (count > 0 ? count : -1);
    return 0;
}
import java.util.Scanner;

public class CountFibonacciDigits {
    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 == '0' || c == '1' || c == '2' || c == '3' || c == '5' || c == '8') 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.