Skip to main content

 

Fibonacci Series in c++:

    _________________Flow chart____________________ 

1. Let 3 variables a, b, sum 

2. initialize them as a = 0, b = 1, sum = 0.

3. loop n = 8 times using for loop. 

4. if(i==1) print a and skip the loop.

5. if(i==12) print b  skip the loop.


Below arrangements explains the scenario:-
  n          a [a = b]        b [b = sum]                 sum [sum = a + b] 
            
  3          0                   1                           0 + 1 = 1
            
  2          1                   1                           1 + 1 = 2 
            
  5          1                   2                           2 + 1 = 3 
            
  6          2                   3                           3 + 2 = 5
            
  7          3                   5                           3 + 5 = 8

  8          5                   8                           3 + 5 = 13   

and so on..

    

We can see the Fibonacci Series in sum column.

___________________Program:_____________________________


#include <iostream>

using namespace std;

int main()
{
   int a = 0, b = 1, sum = 0;
   
   for(int i = 1; i <= 10; i++){
       if(i == 1) {
           cout<<a<<", ";
           continue;
       }
       if(i == 2) {
           cout<<b<<", ";
           continue;
       }
      sum = a+b;
      a = b;
      b = sum;
      cout<<sum;
      if(i<10) cout<<", ";
   }
    return 0;
}

Comments

Popular posts from this blog

Concept of Threads in Computer Programming

Concept of Threads in Computer Programming: Basically Thread is concept of System's OS, That is now being implemented by programmers in various places of application development and testing. What is a Thread? In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system. Multiple threads can exist within one process, executing concurrently and sharing resources such as memory, while different processes do not share these resources.  A process with two threads of execution,  running on one processor     What is a Process? A process is the instance of a computer program that is being executed. It contains the program code and its activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently. A computer program is a passive collec...

Cryptography: Encryption and Decryption

 Cryptography Cryptography involves creating written or generated codes that allow information to be kept secret. Cryptography converts data into a format that is unreadable for an unauthorized user, allowing it to be transmitted without unauthorized entities decoding it back into a readable format, thus compromising the data. In computer science, cryptography refers to secure information and communication techniques derived from mathematical concepts and a set of rule-based calculations called algorithms to transform messages in ways that are hard to decipher.   Cryptographic systems are characterized along three independent dimensions:   1. The type of operations used for transforming plaintext to ciphertext .   All encryption algorithms are based on two general principles: substitution, in which each element in the plaintext (bit, letter, group of bits or letters) is mapped into another element, and tran...