
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find last two digits of Nth Fibonacci number in C++
In this problem, we are given a number N. Our task is to create a Program to find last two digits of Nth Fibonacci number in C++.
Problem Description
We need to find the last two digits (i.e. the two LSB’s ) of the Nth Fibonacci number. Let’s take an example to understand the problem,
Input: N = 120
Output: 81
Solution Approach
A simple solution will be using the direct Fibonacci formula to find the Nth term. But this method will not be feasible when N is a large number. So to overcome this, we will use the property of the Fibonacci Series that last two digits repeats itself after 300 terms. I.e. The last two digits of the 75th term are the same as that of the 975th term.
This means that working till 300 will give us all possible combinations and to find which term to use we will find the number’s mod with 300.
Example
#include <iostream> using namespace std; long int fibo(int N){ long int a=0,b=1,c; for(int i=2; i<= N;i++) { c=a+b; a=b; b=c; } return c; } int findLastTwoDigitNterm(int N) { N = N % 300; return ( fibo(N)%100); } int main() { int N = 683; cout<<"The last two digits of "<<N<<"th Fibonacci term are "<<findLastTwoDigitNterm(N); return 0; }
Output
The last two digits of 683th Fibonacci term are 97
- Related Articles
- Program to find Nth Fibonacci Number in Python
- C++ program to find Nth Non Fibonacci Number
- Program to find Nth Even Fibonacci Number in C++
- Program to find nth Fibonacci term in Python
- Program to find last two digits of 2^n in C++
- Python Program for nth multiple of a number in Fibonacci Series
- Java Program for nth multiple of a number in Fibonacci Series
- Program to find Fibonacci series results up to nth term in Python
- Number of digits in the nth number made of given four digits in C++
- Write a C# function to print nth number in Fibonacci series?
- Program to find nth ugly number in C++
- 8085 program to find nth power of a number
- Find last two digits of sum of N factorials using C++.
- Find the Nth Number Made Up of only Odd Digits using C++
- Golang Program to extract the last two digits from the given year
