Skip to content

Instantly share code, notes, and snippets.

@Zulqurnain
Created December 17, 2013 15:48
Show Gist options
  • Save Zulqurnain/b98de3a6c02a632e8a45 to your computer and use it in GitHub Desktop.
Save Zulqurnain/b98de3a6c02a632e8a45 to your computer and use it in GitHub Desktop.
Custom Square Root Function in c++
#include<iostream.h>
double my_sqrt(int,int);
int main()
{
int num,root; // root can be 2,3,4...
//root depends you want to take square , cube , 4rth , 5th , nth root of any number
cout<<"Enter Your Value N :=:"; cin>>num; cout<<"\n";
if(num<0){
cout<<"Square Root of -ve entry in real Number is Not Possible\n";
}
else{
cout<<"Enter The Root :=:"; cin>>root; cout<<"\n";
double rt=my_sqrt(num,root); // calling custom function
cout<<root<<" Root of Number "<<num<<" :=:"<<rt<<"\n";
}
return 0;
}
double my_sqrt(int Number, int Root)
{
int j;
double i,k=1;
double incre = 0.01;
// 0.01 starting value it is the accuurate value upto 2 decimal places choose 0.01 or 0.001 or so on
// the more the incre is lower the high the accuracy of root !
for(i=1; i<=Number; i+=incre){ // Number n
for(j=0;j<Root;j++){ // any root
k*=i;
}
if(k>Number){ // if multipe is becoming greater then just return its 1 less
return (i-incre);
break;
}
else
k=1; // special case
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment