Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Generate Fibonacci Numbers up to a Limit

Basic Acceptance 81.42% Points 10.00

You are given an integer limit. Your task is to generate and print all Fibonacci numbers less than than or equal to the given limit, starting from 0 and 1.

Each Fibonacci number after the first two is calculated as the sum of the two preceding numbers. Continue generating terms until the next Fibonacci number exceeds the given limit.

Examples
Example 1

Input: limit = 10

Output: 0 1 1 2 3 5 8

Explanation: These are all the Fibonacci numbers that do not exceed 10.

Example 2

Input: limit = 1

Output: 0 1 1

Explanation: The Fibonacci numbers up to 1 are 0, 1, and 1.

Example 3

Input: limit = 0

Output: 0

Explanation: Only the Fibonacci number 0 does not exceed the given limit.

Constraints
  • 0 <= limit <= 1018
Companies
Apple eBay Swiggy
Topics
Data Structures Basics
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions