An efficient way to check whether nth Fibonacci number is multiple of 10 (original) (raw)

Last Updated : 23 Jul, 2025

We are given a variable n, we need to find whether Fibonacci number will be a multiple of 10 or not.

Examples:

Input : 15
Output : Yes

Input : 17
Output : No

A Simple Method is to find the nth Fibonacci number and check if it is divisible by 10 or not.

C++ `

// A simple C++ program to check if // n-th Fibonacci number is multiple // of 10. #include<bits/stdc++.h>

int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i<= n; i++) { c = a + b; a = b; b = c; } return c; }

// Returns true if n-th Fibonacci number // is multiple of 10. bool isMultipleOf10(int n) { int f = fibonacci(30); return (f % 10 == 0); }

// Driver code int main() { int n = 30; if (isMultipleOf10(n)) printf("Yes\n"); else printf("No\n"); }

Java

// A simple Java program to check if // n-th Fibonacci number is multiple // of 10. class Fibonacci { static int fibonacci(int n) { int a = 0; int b=1; int c=0; if (n <= 1) return n;

    for (int i = 2; i<= n; i++)
    {
        c = a + b;
        a = b;
        b = c;
    }
    
    return c;
}

// Returns true if n-th Fibonacci number 
// is multiple of 10.
static boolean isMultipleOf10(int n)
{
    int f = fibonacci(30);
    return  (f % 10 == 0);
}

// main function
public static void main (String[] args) 
{
    int n = 30;
    if (isMultipleOf10(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}

}

Python 3

A simple Python 3 program to check if

n-th Fibonacci number is multiple

of 10.

def fibonacci(n):

a = 0
b = 1
if (n <= 1):
    return n
for i in range(2, n + 1):

    c = a + b
    a = b
    b = c

return c

Returns true if n-th Fibonacci

number is multiple of 10.

def isMultipleOf10(n): f = fibonacci(30) return (f % 10 == 0)

Driver code

if name =="main":

n = 30
if (isMultipleOf10(n)):
    print("Yes")
else:
    print("No")

This code is contributed by ita_c

C#

// A simple C# program to check if // n-th Fibonacci number is multiple // of 10. using System;

class GFG {

static int fibonacci(int n)
{
    int a = 0;
    int b = 1;
    int c = 0;
    if (n <= 1)
        return n;
    
    for (int i = 2; i<= n; i++)
    {
        c = a + b;
        a = b;
        b = c;
    }
    
    return c;
}

// Returns true if n-th Fibonacci
// number is multiple of 10.
static bool isMultipleOf10(int n)
{
    int f = fibonacci(30);
    return (f % 10 == 0);
}

// main function
public static void Main () 
{
    int n = 30;
    
    if (isMultipleOf10(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}

}

// This code contribute by parshar.

PHP

a=0;a = 0; a=0;b = 1; $c; if ($n <= 1) return $n; for ($i = 2; i<=i<= i<=n; $i++) { c=c = c=a + $b; a=a = a=b; b=b = b=c; } return $c; } // Returns true if n-th Fibonacci // number is multiple of 10. function isMultipleOf10($n) { $f = fibonacci(30); return ($f % 10 == 0); } // Driver code $n = 30; if (isMultipleOf10($n)) echo "Yes\n"; else echo "No\n"; // This code is contributed by ajit. ?>

JavaScript

`

Time complexity: O(n)
Auxiliary space: O(1)

Efficient Method :
The above solution may not work if n is very large, then it is not possible to find fibonacci number. Moreover, we can check without finding fibonacci number by looking on the pattern. Let's see how !
If number is divisible by 10, then it must have to be divisible by 5 and 2 both.
Multiples of 2 in Fibonacci Series :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 ....
The number shown in bold are divisible by 2. On careful observation, we finds that every 3rd number is divisible by 2.
Multiples of 5 in Fibonacci Series :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 ......
The number shown in bold are divisible by 5. On careful observation, we find that every 5-th number is divisible by 5.
Now LCM of 3 and 5 is 15. So, every 15-th Fibonacci number will be divisible by 10. So, we don't need to find Fibonacci number, just we have to check if n is divisible by 15 or not.

Below is the implementation.

C++ `

// A simple C++ program to check if // n-th Fibonacci number is multiple // of 10. #include<bits/stdc++.h>

// Returns true if n-th Fibonacci number // is multiple of 10. bool isMultipleOf10(int n) { return (n % 15 == 0); }

int main() { int n = 30; if (isMultipleOf10(n)) printf("Yes\n"); else printf("No\n"); return 0; }

Java

// A simple Java program to check if // n-th Fibonacci number is multiple // of 10. class Fibonacci { // Returns true if n-th Fibonacci number // is multiple of 10. static boolean isMultipleOf10(int n) { if(n%15 == 0) return true;

    return false;    
}

// main function
public static void main (String[] args) 
{
    int n = 30;
    if (isMultipleOf10(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}

}

Python3

A simple Python 3 program to check if

n-th Fibonacci number is multiple

of 10.

Returns true if n-th Fibonacci number

is multiple of 10.

def isMultipleOf10(n):

return (n % 15 == 0)

Driver Code

n = 30

if (isMultipleOf10(n)): print("Yes"); else: print("No");

This code is contributed

by Akanksha Rai

C#

// A simple C# program to check if // n-th Fibonacci number is multiple // of 10. using System;

class GFG {

// Returns true if n-th Fibonacci number 
// is multiple of 10.
static bool isMultipleOf10(int n)
{
    if(n % 15 == 0)
        return  true;
     
    return false;    
}
 
// main function
public static void Main () 
{
    int n = 30;
    if (isMultipleOf10(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}

}

// This code is contributed by nitin mittal.

PHP

JavaScript

// A simple Javascript program to // check if n-th Fibonacci // number is multiple of 10.

// Returns true if n-th // Fibonacci number is // multiple of 10. function isMultipleOf10(n) { return (n % 15 == 0); }

// Driver Code let n = 30; if (isMultipleOf10(n)) document.write("Yes
"); else document.write("No
");

// This code is contributed by _saurabh_jaiswal

`

Time Complexity: O(1) time.
Auxiliary space: O(1)