tags:

views:

139

answers:

4

I made a program to find if a number belongs to fibonacci series or not and if it does whats its position.Whenever i type a number the if Condition goes wrong.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
    int i,x=1,y=1,z,num;
    clrscr();
    printf("Enter a number to find in fibonacci series:");
    scanf("%d",&num);
    /*to find if the number is a part of fibonacci series or not*/
    if((isdigit(sqrt(5*num*num+4)))||(isdigit(sqrt(5*num*num-4))))  //<-- this if!
    {//belongs to fibo!
        for(i=1;    ;i++)
        {
            if(x==num)
            break;
            z=x+y;
            x=y;
            y=z;
        }
        printf("%d is the %d term of fibonacci series.",num,i);
    }
    else
        printf("Dear user,The entered number is not a part of the fibonacci series.");

    getch();
}
+6  A: 

You're misunderstanding the isDigit function.

isDigit takes an ASCII character code and returns true if it represents a decimal digit.

You want to check whether the double returned by sqrt is an integer.

SLaks
i tested isdigit() for a float and it dint worked so i thaught that if the sqrt of the number is a float then it will return 0 and i can accomlish my need from it
fahad
Well, you're wrong. To check whether a number is an integer, you can write a function that casts it to `(int)` and checks whether it's equal to the original value.
SLaks
@SLaks, not all integers can be represented as a float
kibibu
+3  A: 

There's an obvious error in your use of isdigit(). That function (usually macro) is used to tell if a character is one of the characters 0..9 - certainly your code is dealing with numbers consistently and there's no need for character checking.

You'll want to take a closer look at what you're trying to accomplish. You're welcome to ask us which C functions might be suitable.


EDIT:

Ah, you want to know if that funky expression is an integer value. Alas, there's no built-in function for that. I haven't tested this, but I'd write

double a = (funky expr);
if (a == rint(a)) ...

... where rint() is a function that returns the double that's the nearest integer value to the given argument.

Carl Smotricz
how do i find that if the number is square rooted is a int or a float?
fahad
The number returned from `sqrt` is *always* a `double`; the mathematical properties of the returned value don't change the datatype of the variable it's returned in. The question you need to ask is: "Is this number, which is held in a floating point double, equal to an integer, i.e is its fractional part 0?"
Carl Smotricz
+1  A: 

Why are you using isdigit? The result of sqrt is a double - you need to check that value directly.

Justin Ethier
A: 

You want to check if 5 * num * num + 4 or 5 * num * num - 4 is a perfect square. A function that will do this is:

int is_perfect_sq(double d)
{
    double sqroot = rint(sqrt(d));

    return (sqroot * sqroot) == d;
}

Note - this is a good disproof of the notion that you should never compare floating point numbers for equality. In this case, it is fine, since a "perfect square" must be an integer.

caf
hmmm and if one has to compare floats then comparing them by their address will be a good practise?
fahad