Example of a logical error
#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.
Author And Source
이 문제에 관하여(Example of a logical error), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@tonyhan774/Example-of-a-logical-error저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)