Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(void)
- {
- /* ---------Declerations--------- */
- float num; //number to find square root of
- double root; //the result of the calculation
- double max = 1000; //The highest guess
- double min = 0; //The lowest guess
- int imaginary = 0; // 0 for real, 1 for imaginary
- /*---------Input---------*/
- printf("What number would you like to find the square root of? ");
- scanf("%f", num);
- /*---------Calculations---------*/
- if(num<0)
- {
- imaginary = 1;
- num = -num;
- }
- if(num>1)
- {
- max = num; //Highest possible root of a counting number
- }
- else
- {
- max = 1;
- }
- while (max-min > num/1000000000000000)
- {
- root = (max+min)/2;
- if(root*root > num)
- {
- max = root;
- }
- else if(root*root < num)
- {
- min = root;
- }
- else if(root*root == num)
- {
- max=min;
- }
- else
- {
- printf("An error has occured, please contact developer.");
- return 0;
- }
- }
- /*---------Output---------*/
- printf("The square root of %lf is %lf", num, root);
- if(imaginary==1)
- {
- printf("i/n");
- }
- else
- {
- printf("/n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement