Example of a logical error

1069 단어 logical errorCC
#include <stdio.h>

int main(){

    int radius;

    printf("Please enter the radius: ");
    scanf("%i",&radius); 
    int area = 3.14159 * radius * radius;
	//You see the error here?? 
    printf("The given radius is: %i \n",radius);
    printf("The area of the circle is : %i",area);
    
    return 0;
}

In above the code, as we see, int data type is a whole number(not always). But when we multiply 3.14159, it won't be a while number anymore. This is logical error.
The logical error is something that code compiles and works, but the result is not what we were expecting. There is a problem in the code that it leads to an incorrect result.
If we compile as an int, for example, the answer was 65.1231... the 0.1231 part will be truncated. To fix this error, we need a new data type 'double', which is part of the float data type.

좋은 웹페이지 즐겨찾기