Home > Net >  How to remove the last number 0 in the fibonacci series in c ?
How to remove the last number 0 in the fibonacci series in c ?

Time:10-18

I m trying to remove the last 0 in the fibonacci series

as i m removing return 0; the last value is showing garbage value

something like 735150

what should i edit to get the desired output as

0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

as i m getting the output

0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 0

#include <iostream>
using namespace std;

class fibo
{
private:

unsigned long int n1,n2,final;

public:
fibo()
{
    n1 = 0;
    n2 = 1;
    final = n1   n2;
}

fibo(int x1,int x2) //Parameterised Constructor
{
    n1 = x1;
    n2 = x2;
    for (int x = 0;x<=8; x  )
    {
        final = n1   n2;
        cout << final << " ";
        n1 = n2;
        n2 = final;
    }
    
}

int calc()
{
    for (int x = 0;x<=8; x  )
    {
        final = n1   n2;
        cout << final << " ";
        n1 = n2;
        n2 = final;
    }
    return 0;
}

fibo(fibo &i); // Copy Constructor
};

fibo::fibo(fibo &i)
{
n1= i.n1;
n2 = i.n2;
final = i.final;
}

int main()
{
cout << "0 " ;
fibo f1(0,1);
fibo f2 = f1;

cout << f2.calc() << endl;

 return 0;
}

CodePudding user response:

I'm perfectly aware this isn't code review but OP asked for a simpler version.

This is not conforming to the odd copy constructor requirement, but rather to show that printing the fibonacci numbers can be dealt with in a few(8) lines of code.

#include <iostream>
#include <vector>
#include <algorithm>

// This is a poor time to use a classes and objects,
// This is simply a straightforward algorithm.
// calculation is not an object
// As Nathan says, it is a bizarre design

// Generate numbers
std::vector<int> fibonacci_numbers(int N){
    auto n0 = 0; // not used in output
    auto n1 = 1; // first value in output
    auto next_fib = [&n0,&n1](){
        auto value = n1;
        auto next = n0   n1;
        n0 = n1;
        n1 = next;
        return value;
    };
    auto result = std::vector<int>(N);
    std::generate(result.begin(), result.end(), next_fib);
    return result;
}

// Print
void print_fibo(int N){
    int n0 = 0; // not for printing
    int n1 = 1;
    while(N--){
        auto next_fib = n0   n1;
        std::cout << n1 << ", ";
        n0 = n1;
        n1 = next_fib;
    }
}

int main() {
    // seems like you want 19 numbers
    // generating
    auto fibs = fibonacci_numbers(9);
    for(auto e:fibs){
        std::cout << e << ", ";
    }
    // just printing
    std::cout << '\n';
    print_fibo(19);
    std::cout << '\n';
    return 0;
}

CodePudding user response:

I have made some changes as shown in the below code. First you don't need to create a copy of the object f1. You can just directly call f1.calc(). Second calc's return type is changed to void. Third, constructor initializer list is used. You can check the working program here.

#include <iostream>
using namespace std;

class fibo
{
private:

unsigned long int n1,n2,final;

public:
//use constructor initializer list
fibo(): n1(0), n2(1), final(n1 n2)
{
    //n1 = 0;
    //n2 = 1;
    //final = n1   n2;
}

fibo(int x1,int x2) //Parameterised Constructor
{
    n1 = x1;
    n2 = x2;
    for (int x = 0;x<=8; x  )
    {
        final = n1   n2;
        cout << final << " ";
        n1 = n2;
        n2 = final;
    }
    
}

void calc()
{
    for (int x = 0;x<=8; x  )
    {
        final = n1   n2;
        cout << final << " ";
        n1 = n2;
        n2 = final;
    }
    //no need to return
    //return 0;
}

fibo(fibo &i); // Copy Constructor
};

fibo::fibo(fibo &i)
{
n1= i.n1;
n2 = i.n2;
final = i.final;
}

int main()
{
cout << "0 1 " ;
fibo f1(0,1);

//no need to create new object
//fibo f2 = f1;

//call f1.calc() instead of f2.calc()
f1.calc();

 return 0;
}

The output of the above program is:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 

Note there are other ways to implement this. You can check out these here.

Also, in your given example code output, the starting 3 numbers should be 0 1 1 instead of 0 1 2. You can change cout << "0 "; to cout << "0 1 "; to get the correct output 0 1 1 as i did in my edit.

  •  Tags:  
  • c
  • Related