#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    43
    Rep Power
    4

    Perfect Square function not working properly?


    It should return false when the number does not have perfect square, otherwise it returns the root. But in this code it returns the root all the time. E.g. Input 5, root 2.

    Code:
        main()
        {
        int i;
        int number=0;
        int result=0;
    
        for(i=0; i<10; i++){
            printf("Testing:");
            scanf("%i",&number);
    
    
            result = isSquare(number);
    
            if(result==0)
                printf("Fail\n");
            else
                printf("%i\n",result);
    
        }
        }
    
    
        int isSquare(int n)
        {
        float root = sqrt(n);
        if (n == (int) n)
            return root;
    
        else
            return 0;
    
        }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Note what you're testing here.

    Code:
    float root = sqrt(n);
        if (n == (int) n)
            return root;

IMN logo majestic logo threadwatch logo seochat tools logo