1 Replies - 618 Views - Last Post: 18 November 2012 - 09:15 PM Rate Topic: -----

#1 hey92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-November 11

Perfect square in fibonacci sequence program not working

Posted 18 November 2012 - 06:46 PM

"Create a program to find out the first perfect square greater than 1 to occur in the Fibonacci sequence and display it to the console." I have no output when I enter an input.



#include <stdio.h>
#include <math.h>

int PerfectSquare(int n);
int Fibonacci(int n);

main()
{
    int i;
    int number=0;

        int fibNumber=0;
        int psNumber=0;


        printf("Enter fibonacci number:");
        scanf("%i",&number);


        fibNumber = Fibonacci(number);

        psNumber = PerfectSquare(fibNumber);

        if(psNumber != 0){
        printf("%i\n",psNumber);
        }
}



int PerfectSquare(int n)
{

float root = sqrt(n);
if (n == ((int) root)*((int) root))
    return root;
else
    return 0;
}

int Fibonacci(int n){
    if (n==0) return 0;
    if (n==1) return 1;
    return( Fibonacci(n-1)+Fibonacci(n-2) );
}



Is This A Good Question/Topic? 0
  • +

Replies To: Perfect square in fibonacci sequence program not working

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Perfect square in fibonacci sequence program not working

Posted 18 November 2012 - 09:15 PM

View Posthey92, on 19 November 2012 - 07:16 AM, said:


int PerfectSquare(int n)
{

float root = sqrt(n);
if (n == ((int) root)*((int) root))
    return root;
else
    return 0;
}



Can you please explain what you are trying to do here?

The reason for you not getting any output probably lies in this very part.

regards,
Raghav

This post has been edited by raghav.naganathan: 18 November 2012 - 09:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1