Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Generate Fibonacci Numbers up to a Limit

Basic Acceptance 81.42% Points 10

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
Topics
Data Structures Basics
Companies
Apple eBay Swiggy
Added 01 Nov 2026 81.42% acceptance
Ln 1, Col 1Sign in to save your code
10
0 1 1 2 3 5 8