Online Compiler C

#include <stdio.h> float cosinus(float x) { float sum = 1; float term = 1; int n = 0; while (term >= 0.001||term <= -0.001) ; { n = n + 2; term = -term * x * x / (n * (n - 1)); sum = sum + term; } return sum; } int main() { float degree; scanf("%f", &degree); float radian = degree * 3.14159265 / 180; printf("%.3f\n", cosinus(radian)); return 0; }
This code attempts to compute the cosine of an angle using a Taylor series expansion. It converts degrees to radians, then sums terms of the series until the term magnitude is below 0.001. However, there is a problem with the loop structure.

- The while loop condition has a semicolon right after it, creating an empty loop body that runs forever. The intended loop body (the block with braces) is placed after the semicolon and is not part of the loop.
- The loop condition uses `||` with both `>= 0.001` and `<= -0.001`. This condition is always true because any number is either >= 0.001 or <= -0.001. You likely want to check the absolute value of the term.