C dilinde karekök alma programı.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <stdio.h> #include <math.h> double karekok(double); int main() { while(true) { printf("Press -1 to exit: \n"); double n; printf("Enter a number to find its squareroot: \n"); scanf_s("%lf", &n); if (n == -1) { return 0; } if (n == 1 || n == 0) { printf("Result: %lf", n); } while (n < 0) { printf("You entered invalid number. Please enter again:"); scanf_s("%lf", &n); } double result = karekok(n); printf("Squareroot of the number is %lf\n", result); } return 0; } double karekok(double n) { double min = 0; double max = n; double currentVal; double epsilon = 0.0000001; while (true) { currentVal = (min + max) / 2.0; if ( fabs(pow(currentVal,2) - n) <= epsilon) { return currentVal; } else { double x = pow(currentVal, 2) - n ; if (x < 0) { min = currentVal; } else { max = currentVal; } } } } |