finding square root in php for 30 credits

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
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)
 

de.monkeyz

New Member
Messages
35
Reaction score
0
Points
0
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.
 

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
please dont use inbuilt php functions there arent any such things in c language u see!
 

winlux

New Member
Messages
49
Reaction score
0
Points
0
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
 

de.monkeyz

New Member
Messages
35
Reaction score
0
Points
0
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:

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
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);
}
 

de.monkeyz

New Member
Messages
35
Reaction score
0
Points
0
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:

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
oh! i dont have iostream in my compiler and also the code is way beyond my recognition
 

de.monkeyz

New Member
Messages
35
Reaction score
0
Points
0
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:

de.monkeyz

New Member
Messages
35
Reaction score
0
Points
0
Hmmm sorry can't really help... I'm usin' a c++ compiler so I can't check it out. Hope you find your answer
 
Top