1. ## Fibbonacci Code

Hey all, Programming newbie here.

I'm programming the Fibbonacci code, but I can't figure out how to set the condition

"if NOT EQUAL to any numerical value"

This is what I have so far:

Code:
```#include <stdio.h>
#include <conio.h>

long fib(int);

int main()
{
int n;

printf("Enter the Fibbonacci number you would like to view: ");
scanf("%i", &n);

if (WHAT GOES HERE?)
{
printf("Input error.\n");
getch();
}
else if (n<0) {
printf("Fibonacci is undefined for negative integers.\n");
getch();
}
else {
printf("The %i-th Fibonacci number is %ld\n", n, fib(n));
}
getch();
return 0;
}

long fib(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return (fib(n-1) + fib(n-2));
}
}```
Thanks for the help in advance!

2. Was writing the following in the other thread when it was closed - might be what you're looking for:

fgets and strtol is the way I'd do it, but you can use scanf cleverly too:

Code:
```int num = 0;

if (!scanf ("%d", &num))
{
printf ("Invalid input");
}```
this code will try to read in an integer from the value entered - if a character is entered, it will print the error message - however, if the value entered is something like 213g, it will read in the 213 part, and if the value was like 12.2, it will read in the 12 part - not bullet-proof, but it might do for what you need.

3. Thanks Richie, I was trying your code, but every return was "the 2th number is 1". I think it just keeps refering to the base value for some reason

But, I was playing with it, and this one worked somehow :S

Code:
```if (scanf("%i", &n) != 1) {
printf("Input error.\n");
getch();```
Could someone explain Why it worked? From what I understand, it means "If the inputted integer is not equal to 1, print "input error" (which makes no sence to me, since 1 is an integer...)

4. scanf returns the number of values successfully read in - since you are using it to read in one value, if it fails, it'll return 0 (which is not equal to 1)

5. Thank you VERY Much. ALl's working fine, now just to inprove the GUI. Thanks to all who've helped me!

Final Code
Code:
```#include <stdio.h>
#include <conio.h>

long fib(int);

int main()
{
int n;
int num;

num = 0;

printf("Enter the Fibbonacci number you would like to view: ");

if (scanf("%i", &n) != 1)
{
printf("Input error.\n");
getch();
}
else if (n<0) {
printf("Fibonacci is undefined for negative integers.\n");
getch();
}
else {
printf("The %i-th Fibonacci number is %ld\n", n, fib(n));
}
getch();
return 0;
}

long fib(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return (fib(n-1) + fib(n-2));
}
}```

6. Why the non-standard code? Dump the conio.h... change all the getch() calls to getchar() calls... it's that simple.

Code:
```
long long int fibo( int n )
{
long long int* mem = new long long int[n +1];
long long int ret;

mem[1] = mem[2] = 1;

for (int i = 3 ; i <= n ; i ++)
{
mem[i] = mem[i -1] + mem[i -2];
}

ret = mem[n];

delete[] mem;
return ret;
}```

8. Originally Posted by theFOX

Code:
```
long long int fibo( int n )
{
long long int* mem = new long long int[n +1];
long long int ret;

mem[1] = mem[2] = 1;

for (int i = 3 ; i <= n ; i ++)
{
mem[i] = mem[i -1] + mem[i -2];
}

ret = mem[n];

delete[] mem;
return ret;
}```
Ehem... C board, C++ code

9. uhm, sorry

Code:
```long long int fibo( int n )
{
long long int* mem;
mem = (long long int*) calloc( 0, sizeof(long long int) * (n +1) );

long long int ret;

mem[1] = mem[2] = 1;

int i;
for ( i = 3 ; i <= n ; i ++)
{
mem[i] = mem[i -1] + mem[i -2];
}

ret = mem[n];

free( mem );
return ret;
}```

10. Read the FAQ for why you shouldn't cast *alloc().

11. There are only 46 Fibonacci numbers that can fit within a 32-bit unsigned type. Use a table.

12. Originally Posted by Prelude
There are only 46 Fibonacci numbers that can fit within a 32-bit unsigned type. Use a table.
Is that also true for calculating factorial?

13. >Is that also true for calculating factorial?
Factorial is even easier, you can only fit 12! into a 32-bit unsigned int.

14. Yeah, also windows calculator gives me warnings whenever I try to do 1,000,000!. It says that it will take a long time. I've not out waited it yet :/ I hope to do so some time.

15. Originally Posted by Prelude
There are only 46 Fibonacci numbers that can fit within a 32-bit unsigned type. Use a table.
Actually 48

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073