×

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP

Definition:

Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements. But, there's a logical difference in Fibonacci Triangle. Typically, Fibonacci Triangle is a triangular arrangement (like Pascal's Triangle). In Fibonacci Triangle, each number is the sum of the two numbers covering left diagonals or right diagonals.

C++ Program to Print Fibonacci Triangle

After rearranging the triangle, it may look like

C++ Program to Print Fibonacci Triangle

Note: A Fibonacci Triangle is also called as Hasoya’s Triangle.

Various recurrence relations properly explain the logic behind it.

Consider a Fibonacci recurrence relation

H(0, 0) = H(1, 0) = H(1, 1) = H(2, 1) = 1

The numbers in the triangle obey recurrence relations.

Also,

 H(n, j) = H(n ? 1, j) + H(n ? 2, j)
 = H(n ? 1, j ? 1) + H(n ? 2, j ? 2). 

 From these relations, we can conclude that:

  1. The outermost diagonals are Fibonacci Numbers.
  • The numbers on middle vertical line are squares of     Fibonacci Numbers.

Let us look at the code below.

 #include<bits/stdc++.h>
 using namespace std;
 int FibonacciTriangle(int number)
 {
     int dp[N][N];
     memset(dp, 0, sizeof(dp));
     dp[0][0] = dp[1][0] = dp[1][1] = 1;
     for (int i = 2; i < number; i++) {
         for (int j = 0; j < number; j++) {
             if (i > j)
                 dp[i][j] = dp[i - 1][j] + dp[i - 2][j];
             else
                 dp[i][j] = dp[i - 1][j - 1] + dp[i - 2][j - 2];
         }
     }
     for (int i = 0; i < number; i++) {
         for (int j = 0; j <= i; j++) 
             cout << dp[i][j] << " ";        
         cout << endl;
     }
 }
 int main()
 {
     int number;
     cin>>number;
     FibonacciTriangle(number);
 return 0;
 } 

Output:

C++ Program to Print Fibonacci Triangle

Explanation:

The above code is a representation of the Fibonacci Triangle. It follows various programming approaches usually dynamic programming.

Function Fibonacci Triangle takes a number from the user and the function is defined as a 2D array that takes values in the forms of rows and columns. The row and column are assigned 0 and 1 value initially. According to the following formula

             H(n ? 1, j ? 1) + H(n ? 2, j ? 2).

Here, if the input is taken as 6, it will print Fibonacci Numbers in the sequence of rows and columns based on either left or right diagonals.

The two loops are defined to iterate over rows and columns. Since the array is multi-dimensional, we can use dynamic programming to assign values in a sequential arrangement that satisfies the above formula for Fibonacci numbers. It ensures that the outermost diagonals are Fibonacci numbers and the middle vertical elements are the square of Fibonacci Numbers.

The next loop iterates through the values in rows and columns and prints them on the console.

Numbers in Characters in C++

It is a popular programming practice where we are supposed to generate strings respective to the digits entered. In order words, numbers in characters simply mean to convert numbers into the characters like they are called.

Example:

 Given an integer N. Task is to convert number in character

Input: 12345

Output: one two three four five

Code:

 #include<bits/stdc++.h>
 using namespace std;
 int NumChar(int number)
 {
     int reverse = 0, r = 0;
     while (number > 0) {
         r = number % 10;
         reverse = reverse * 10 + r;
         number = number / 10;
     }
     while (reverse > 0) {
         r = reverse % 10;
         switch (r) {
         case 1:
             cout << "one ";
             break;
         case 2:
             cout << "two ";
             break;
         case 3:
             cout << "three ";
             break;
         case 4:
             cout << "four ";
             break;
         case 5:
             cout << "five ";
             break;
         case 6:
             cout << "six ";
             break;
         case 7:
             cout << "seven ";
             break;
         case 8:
             cout << "eight ";
             break;
         case 9:
             cout << "nine ";
             break;
         case 0:
             cout << "zero ";
             break;
         default:
             cout << "Invalid ";
             break;
         }
         reverse = reverse / 10;
     }
 }
 int main()
 {
     int number;
     cin>>number;
     NumChar(number);
     return 0;
 } 

Output:

C++ Program to Print Fibonacci Triangle

Explanation:

Start

  1. Define a function taking integer input.
  2. Reverse the number.
  3. Carry out right to left iteration.
  4. Find last digit with the help of modulus operator.
  5. Divide the number by 10 to get the factor.
  6. Switch case to find the corresponding character.
  7. Call the function in the drive code with the number entered.

Note: We can also use loops but that will increase time complexity.


Related Topics

Tensorflow in C++

With cppflow, you can execute TensorFlow models in C++ without Bazel, TensorFlow installation, or TensorFlow compilation. Tensor manipulation, eager execution, and running stored models straight from C++ are all possible. Knowing...

7 minutes read.

C++ If

C++ Control Statement C++ control statement or decision-making statement is used to control the flow of program statement according to condition applied. C++ if Control Statement An if control statement in C++ is used to...

2 minutes read.

SDL library in C++ with Examples

SDL stands for Simple DirectMedia Layer. Using OpenGL and Direct3D, it is a cross-platform development library created to give users low-level access to audio, keyboard, mouse, joystick, and graphics hardware....

3 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

Implementation of a Falling Matrix in C++

In many Hollywood and Bollywood movies, we might have seen a programmer who will be referred to as a hacker all the time for some random reason which the writer...

3 minutes read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

3 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

Differences between #define & const in C/C++

 Differences between #define & const in C/C++ A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation. In this chapter, we'll learn about the member, variable,...

3 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

C++ Call by Value

In this article, we will discuss C++ Call by Value with their syntax, examples, use cases, advantages, and disadvantages. C++ Function A function is a set of statements that executes a task....

5 minutes read.

C++ Heap Sort

Heapsort is executed on the structure of the heap data. We know heap is a complete tree in binary form. The heap tree can be of two different types: Min-heap,...

3 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Divide by Zero Exception in C++

We use exception handling method to handle the divide by zero exception. Dividing a number with zero is generally mathematical error. We have to exception handling method to overcome this...

2 minutes read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

3 minutes read.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

Hospital Management Project in C++

The following capabilities are required to build a hospital management project: These are as follows: hospitals' names, contact information, and lists of doctors and patients. Activities Supported Hospital Data Print Patients' data to...

4 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.