Last Updated: Mar 27, 2024
Difficulty: Easy

# Finding Fibonacci Numbers in Carbon

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

## Introduction

You must have recently heard about Fibonacci Numbers. You might have even written programs in various languages to determine the first N Fibonacci Numbers or the Nth Fibonacci Number.

In this article, we will do the same, but with a new language called Carbon that Google has recently developed.

## Fibonacci Numbers

Fibonacci Numbers are a number series in which a number is the sum of two of its preceding numbers. The first two numbers in the series are 0 and 1.

For example, the first five numbers in the Fibonacci Series are

``0, 1, 1( = 0+1), 2( = 1+1), and 3( = 2+1).``

Some common applications of Fibonacci numbers can be seen in computer algorithms such as the Fibonacci Search Technique. Fibonacci Heaps are also among the advanced data structures which have wide usages. Graphs called Fibonacci Cubes are also used to interconnect distributed and parallel systems.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Carbon

Carbon is an experimental general-purpose programming language. Carbon-Lang is created as a successor to the popular language C++. It is an open-source project developed by Google.

Carbon has yet to be fully developed, and limited communities are available for this language. It intends to compensate for several shortcomings of C++, keeping most of the functionality the same.

## Problem Statement

Find the first N numbers in the Fibonacci Number Series in Carbon Language.

## Solution

As we know that the first two Fibonacci Numbers of the Fibonacci Series are 0 and 1, we can determine the next term by taking the sum of these two and determining the remaining numbers.

### Iterative Approach

Let us look into the iterative approach to solve the problem.

Algorithm

``````n = number of terms to be calculated

// Initialization term
a = -1

// Initialization term
b = 1

while (number of terms !=0)
Temp = a+ b
print(Temp)
a = b
b = Temp
n = n-1``````

Here the Temp variable stores the terms for the Fibonacci Numbers or the Fibonacci Series. Since we only need the last two terms to calculate the following Fibonacci Number, we only keep track of the last two Fibonacci Numbers. Doing so saves a lot of additional space.

For the determination of initial values, we can easily determine by basic mathematics that a = -1 and b = 1 initially.

Now, let us take a look at the implementation of this algorithm in Carbon:

Implementation

``````package sample api;
fn Main() -> i32 {

// The number of terms to be printed
var n:i32 = 10;

// Initialization value
var a:i32 = -1;

// Initialization value
var b:i32 = 1;

var c:i32;
while(n > 0) {
// Calculating the term in the sequence
c = a+b;
a=b;
b=c;
Print("{0}",c);
n = n-1;
}
return 0;
}``````

Output

### Recursive Approach

Let us look into the recursive approach to solve the problem.

Algorithm

``````// Calculates the nth Fibonacci number
Function Fibo(n)
If n<=1
Return n

// Sum of previous two Fibonacci Numbers
Return Fibo(n-1)+Fibo(n-2)

Function main
n = number of terms to be calculated
While n!=0
// Nth Fibonacci Number
Number = Fibo(n)
print(Number)
N = n-1``````

In this approach, the Fibo function is called recursively. To determine the Nth Fibonacci Number, Fibo(n) function is called, and then until the base case (here n<=1) is achieved, Fibo(n-1)+Fibo(n-2) is returned which is nothing but the sum of two previous terms in the Fibonacci Series.

Implementation

``````package sample api;

fn Fibo(var n:i32) ->i32 {
if(n<=1) {
// Base case
return n;
}
// Sum of the previous two Fibonacci Numbers
return Fibo(n-1)+Fibo(n-2);
}

fn Main() -> i32 {
// Number of terms to be calculated
var n:i32 = 7;
while(n > 0) {
Print("{0}",Fibo(n));
n = n-1;
}
return 0;
}``````

Output

### What is the difference between Carbon and C++?

While the fundamental differences can only be speculated about, Carbon promises to cover all the functionalities provided by C++ and improve on its shortcomings.

### Is Carbon an Object-Oriented Language?

Carbon has support for Object Oriented Programming, just like C++.

### What are some other languages developed by Google?

Google has brought us open-source languages like Dart and developed a framework known as AngularJS, which is used for web development and various other things.

### Can I contribute to the development of Carbon Language?

Yes! Since Carbon Language is open-source, anyone can contribute to its development.

You can refer to the official documentation or keep an eye out for blogs from CodingNinjas.

## Conclusion

In this article, we discussed the Fibonacci Number Series and briefly discussed Carbon Language. We also wrote a program to determine the first N Fibonacci Numbers in Carbon.

Don't stop here. Check out our Data Structures and Algorithms-guided path to learn Data Structures and Algorithms from scratch. Also, check out some Interview Experiences and Contests only on Coding Ninjas Studio.

Topics covered
1.
Introduction
2.
Fibonacci Numbers
3.
Carbon
4.
Problem Statement
5.
Solution
5.1.
Iterative Approach
5.2.
Recursive Approach
6.