Example of a logical error

Han sang yeob·2021년 1월 28일
0

Basic of C language

목록 보기
2/9
#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.

profile
Learning IT in Vietnam

0개의 댓글

관련 채용 정보