# Display number in Fibonacci sequence

Page 1 of 1

## 12 Replies - 750 Views - Last Post: 10 February 2020 - 05:11 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417993&amp;s=10f6993f3799a2c8f93cd1f0e11b8dac&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 J54

Reputation: 0
• Posts: 27
• Joined: 22-December 18

# Display number in Fibonacci sequence

Posted 05 December 2019 - 12:16 PM

ive got this code but it needs tweaking can some one help me out.

After entering a number in a textbox the code will display that number in the Fibonacci sequence in another textbox
```namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnCalculate_Click(object sender, EventArgs e)
{
int N = int.Parse(txtNumber.Text);

double d2 = 0;
double d1 = 1;
double fib = d1 + d2;

for (int i = 3; i < N; i++)
{
d2 = d1;
d1 = fib;
fib = d1 + d2;
}

txtFibo.Text = fib.ToString();
}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Display number in Fibonacci sequence

### #2 modi123_1

• Suitor #2

Reputation: 15497
• Posts: 62,059
• Joined: 12-June 08

## Re: Display number in Fibonacci sequence

Posted 05 December 2019 - 12:23 PM

Tweaking to do what? You neglected to ask a question.

### #3 J54

Reputation: 0
• Posts: 27
• Joined: 22-December 18

## Re: Display number in Fibonacci sequence

Posted 05 December 2019 - 12:35 PM

it doesn't produce the right result my teacher said. im thinking one of the numbers is off

you enter a number in a textbox and then it displays the number in fib sequence in another textbox.

### #4 modi123_1

• Suitor #2

Reputation: 15497
• Posts: 62,059
• Joined: 12-June 08

## Re: Display number in Fibonacci sequence

Posted 05 December 2019 - 12:40 PM

What happens when you run it and verify the output? Have you actually done the comparison to a known list?

Ex: https://www.mathsisf...i-sequence.html

### #5 J54

Reputation: 0
• Posts: 27
• Joined: 22-December 18

## Re: Display number in Fibonacci sequence

Posted 05 December 2019 - 12:46 PM

when you run it it produces a number not in the correct order

### #6 Skydiver

• Code herder

Reputation: 7239
• Posts: 24,542
• Joined: 05-May 12

## Re: Display number in Fibonacci sequence

Posted 05 December 2019 - 12:52 PM

0 should result in 0. You currently output 1.
1 should result in 1. You currently output 1.
2 should result in 1. You currently output 1.
3 should result in 2. You currently output 1.
4 should result in 3. You currently output 2.
:

### #7 J54

Reputation: 0
• Posts: 27
• Joined: 22-December 18

## Re: Display number in Fibonacci sequence

Posted 05 December 2019 - 01:08 PM

i got the answer to this needed <=
```namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnCalculate_Click(object sender, EventArgs e)
{
int N = int.Parse(txtNumber.Text);

double d2 = 0;
double d1 = 1;
double fib = d1 + d2;

for (int i = 3; i <= N; i++)
{
d2 = d1;
d1 = fib;
fib = d1 + d2;
}

txtFibo.Text = fib.ToString();
}
}
}

```

### #8 modi123_1

• Suitor #2

Reputation: 15497
• Posts: 62,059
• Joined: 12-June 08

## Re: Display number in Fibonacci sequence

Posted 05 December 2019 - 01:13 PM

So the issue was it was not printing out the nth one, but instead the n-1th one?

A far cry from 'not producing the right sequence', but cool beans.

### #9 Skydiver

• Code herder

Reputation: 7239
• Posts: 24,542
• Joined: 05-May 12

## Re: Display number in Fibonacci sequence

Posted 05 December 2019 - 03:00 PM

J54, on 05 December 2019 - 03:08 PM, said:

i got the answer to this needed <=

Still doesn't work for Fib(0)

### #10 fearfulsc2

• D.I.C Regular

Reputation: 18
• Posts: 301
• Joined: 25-May 16

## Re: Display number in Fibonacci sequence

Posted 18 December 2019 - 07:25 AM

a simple solution you can think about for the fib sequence is that for the first two iterations, the values are 0 and 1 respectively. Those are your base cases.

Depending on the requirements, the first number in the sequence is 0 and the second is 1. Unless you want to say the zeroth number is 0 and the first number is 1.

By that logic, your code can do this in the beginning

```// for first number in fib sequence
if(N == 1)
return 0;
// for second number in fib sequence
if (N == 2)
return 1;

// will add firstNum and secondNum and have the temp variable as a placeholder.
// there is a different solution where we wouldn't need a third variable but I'll stick to this for now.
int firstNum = 0;
int secondNum = 1;
int temp;

// we are starting with i = 3 since we already know
// where the first 2 values are. No need to do the calculations again
for (int i = 3; i <= N; i++)
{
temp = firstNum + secondNum;
// for the next calculation, we will need to change the value of firstNum and secondNum
firstNum = secondNum;
secondNum = temp;
}

return secondNum; // or we could also return temp since it's also the same value (that is why we could do another solution)
//but I did this one as it might be the simplest to follow

// output
// N = 3 -> 1
// N = 4 -> 2
// N = 5 -> 3
// N = 6 -> 5
// N = 7 -> 8
// N = 8 -> 13
// N = 9 -> 21
// N = 10 -> 34
// etc....

```

I have not tested the output of this but it should be along the lines of this if you follow the logic behind it.

This post has been edited by fearfulsc2: 18 December 2019 - 07:26 AM

### #11 baavgai

• Dreaming Coder

Reputation: 7506
• Posts: 15,556
• Joined: 16-October 07

## Re: Display number in Fibonacci sequence

Posted 19 December 2019 - 03:19 AM

So, um, you haven't fixed your logic, you've just short circuited it?

To test your code, I wrote this:
```class Program {
// wtf do you use double?!?
private static double YourFib(int N) {
double d2 = 0;
double d1 = 1;
double fib = d1 + d2;
for (int i = 3; i <= N; i++) {
d2 = d1;
d1 = fib;
fib = d1 + d2;
}
return fib;
}

static void Main(string[] args) {
for(int n=0; n<10; n++) {
Console.WriteLine(\$"fib({n}) = {YourFib(n)}");
}
}
}

```

Result:
```fib(0) = 1
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
fib(8) = 21
fib(9) = 34

```

Well, that ain't right. See if you can't get it spitting out the right numbers. And get rid of the double. Your final solution could looks something like:
```private int Fib(int n) { /* your code here */ }

private void btnCalculate_Click(object sender, EventArgs e) {
txtFibo.Text = Fib(int.Parse(txtNumber.Text)).ToString();

}

```

Hope this helps.

### #12 ishan.shah

Reputation: 0
• Posts: 21
• Joined: 28-March 19

## Re: Display number in Fibonacci sequence

Posted 10 February 2020 - 01:45 AM

```public static void Main(string[] args) {
int a= 0, b= 1, c, i, n;
n=7;

Console.WriteLine("fibonaci serics:");
Console.Write(a+" "+b+" ");

for(i=2;i<n;++i)
{
c= a+ b;
Console.Write(val3+" ");
val1 = val2;
val2 = val3;
}

```

here no of limit are set by n variable

put the debugger point for better understanding

first, check the loop after that sum of a+ b and store into c then print the values of c

This post has been edited by Skydiver: 10 February 2020 - 05:12 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.

### #13 Skydiver

• Code herder

Reputation: 7239
• Posts: 24,542
• Joined: 05-May 12

## Re: Display number in Fibonacci sequence

Posted 10 February 2020 - 05:11 AM

Where did val1, val2, and val3 get declared and initialized?