/* * lt1o.c Laboratory test I for students working on odd number machines. */ #include float pow2p5(float); int main() { int n, m, absN; // Task 1 printf("Enter an integer: "); scanf("%d", &n); if(n%17 == 0) m = n; else if(n > 0) m = n/17*17; else m = (n - 17) - n%17; printf("\nm: %d \n", m); // Task 2 absN = (n < 0)? -n:n; if(absN == 0) printf("Digits of 0 = (0)\n"); else { printf("Digits of %d: (", n); while(absN){ printf("%d ", absN % 10); absN = absN / 10; } printf("\b)\n"); } // Task 3 absN = (n < 0)? -n:n; if(absN == 0 || absN == 1) printf("%d is the largest Fibonacci no <= |%d|\n", absN, n); else { int f0=0, f1=1, i; for(i=2; ; ++i){ f1 = f0+f1 ; f0 = f1-f0; if(f1 > absN) break; } printf("%d is the largest Fibonacci no <= |%d|\n", f0, n); } // Task 4 printf("%d^2.5 = %f\n", (n < 0)?-n:n, pow2p5((n < 0)?-n:n)); return 0; } float pow2p5(float x){ float x0, x1, err, x1Abs ; if(x == 0.0) return 0.0; x0 = x/2; do { x1 = x0 - (x0*x0 - x)/2.0/x0; err = x1 - x0; err = (err < 0)? -err: err; x1Abs = (x1 < 0)? -x1: x1; x0 = x1; } while(err > 0.0001*x1Abs/100.00); return x*x*x1; }