+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: finding square root in php for 30 credits

  1. #1
    nahsorhseda's Avatar
    nahsorhseda is offline x10 Sophmore nahsorhseda is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    mumbai
    Posts
    116

    finding square root in php for 30 credits

    does any one know how to find the square root of a number. with php
    for example if i input 25 then i must get the result 5,and if the input is 6(which does not have a s.root),it must echo invalid no.
    i hope you understood,i want the script to be simple ,really really simple ,u may use stuff such as if-else ,all 3 loops,dont use anything such as arrays or more complicated stuff

    iam going to convert the php script to "c" language ,so if u can write the program directly in c ,i would be really thankfully

    the script sounds simple but i wasted nearly 6 hrs but no success,hope some one helps me
    (credits=30)
    glad to resolve problems in c++ n php

  2. #2
    de.monkeyz is offline x10Hosting Member de.monkeyz is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    35

    Re: finding square root in php for 30 credits

    The script is quite easy, it would go like this in php, not totally sure in C;


    $value = $_GET['value'];

    $root = sqrt($value);

    if(is_int($root))
    echo $root;
    else
    echo "Invalid, No square root";


    Hope that helps.

  3. #3
    nahsorhseda's Avatar
    nahsorhseda is offline x10 Sophmore nahsorhseda is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    mumbai
    Posts
    116

    Re: finding square root in php for 30 credits

    please dont use inbuilt php functions there arent any such things in c language u see!
    glad to resolve problems in c++ n php

  4. #4
    winlux is offline x10Hosting Member winlux is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    49

    Re: finding square root in php for 30 credits

    It shouldn't be too different from the php you must declare variables, write to them and then read from them. Maybe have a look at sites like www.codeproject.com or similar. They normally have these types of tutorials
    Rate my site and pm me with comments

    http://avtheory.co.cc

  5. #5
    de.monkeyz is offline x10Hosting Member de.monkeyz is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    35

    Re: finding square root in php for 30 credits

    You can use sqrt in C if you include Math.h, as for is_int you can try if(root/1.00 == (int)value); where value is a double. Ill see if I can type the code for ya.

    #include <Math.h>
    #include <iostream>

    using namespace std;

    int main(void)
    {
    double value, root;

    cin>>value;

    root = sqrt(value);

    if(root/1.00 == (int)root)
    cout<<root;
    else
    cout<<"Invalid Number"<endl;
    }

    sorry if it isnt very good, I use c++ more than c, so I dont know if they all work.
    Last edited by de.monkeyz; 03-24-2008 at 07:29 AM. Reason: forgot to add main function

  6. #6
    nahsorhseda's Avatar
    nahsorhseda is offline x10 Sophmore nahsorhseda is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    mumbai
    Posts
    116

    Re: finding square root in php for 30 credits

    i really dont know c++ and the code is much too complicated for me to understand

    i tried for example the below code please correct for any possible errors
    #include<stdio.h>
    #include<math.h>
    main()
    {

    float a,b;
    clrscr();
    a=100;
    b=sqrt(a);
    printf("%d",b);
    }
    glad to resolve problems in c++ n php

  7. #7
    de.monkeyz is offline x10Hosting Member de.monkeyz is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    35

    Re: finding square root in php for 30 credits

    Sorry I don't know how to use stdio.h, I use iostream, which is much simpler to use in my opinion. I'm not sure how it works sorry. C++ is exactly C really except it supports OOP, there are more differneces but they don't really effect this code.

    #include <iostream>
    #include<math.h>

    int main()
    {

    float a,b;
    a=100;
    b=sqrt(a);

    if(b == (int)b)
    std::cout<<b;
    else
    std::cout<<"Invalid No.";

    return 0;
    }

    Is as simple as I can make im afraid
    Last edited by de.monkeyz; 03-24-2008 at 08:04 AM.

  8. #8
    nahsorhseda's Avatar
    nahsorhseda is offline x10 Sophmore nahsorhseda is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    mumbai
    Posts
    116

    Re: finding square root in php for 30 credits

    oh! i dont have iostream in my compiler and also the code is way beyond my recognition
    glad to resolve problems in c++ n php

  9. #9
    de.monkeyz is offline x10Hosting Member de.monkeyz is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    35

    Re: finding square root in php for 30 credits

    Hmmm.... then I don't know I checked out that code but the printf function kept outputting 0 to the window. And clrscr or whatever didn't exist, I also made sure that main had a return type of void.
    Edit:
    Since you like your code, this works
    Code:
    #include<stdio.h>
    #include<math.h>
    
    void main()
    {
    
        float a,b;
    
        a=100;
        b=sqrt(a);
    
        if(b == (int)b)
    	printf("%u",(int)b);
        else
    	printf("Invalid No.");
    
    }

    (int) just datatypes it to an integer temporarily, so that should work in your compiler.
    Last edited by de.monkeyz; 03-24-2008 at 08:26 AM. Reason: Automerged Doublepost

  10. #10
    nahsorhseda's Avatar
    nahsorhseda is offline x10 Sophmore nahsorhseda is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    mumbai
    Posts
    116

    Re: finding square root in php for 30 credits

    iam getting a error saying
    line 12: unrecognised types in comparison
    'if(b == (int)b) printf("%u",(int)b)'
    aborting compile
    glad to resolve problems in c++ n php

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 10:07 AM
  2. [PHP] PHP For Starters
    By Complex in forum Tutorials
    Replies: 24
    Last Post: 06-14-2008, 11:40 PM
  3. Look at my sites and get credits!
    By Scott B in forum Review My Site
    Replies: 36
    Last Post: 06-01-2008, 06:28 PM
  4. Sports fans! Up to 100 credits
    By intheclutch in forum The Marketplace
    Replies: 10
    Last Post: 03-05-2008, 06:05 PM
  5. Earn 150+ Credits Answering my question....
    By admintwo in forum The Marketplace
    Replies: 23
    Last Post: 02-15-2008, 07:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers