The difficulty of getting started with Huawei's machine test-take an approximate value (C language)

Title description:
Write a program that accepts a positive floating point value and outputs the approximate integer value of the value. If the value after the decimal point is greater than or equal to 5, it is rounded up; if it is less than 5, it is rounded down.

Input description:
Input a positive floating point value

Output description:
output the approximate integer value of the value

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main(){
    
    
	float num;
	int a, b;
	scanf("%f", &num);
	a = (int)num;
	b = (int)(num * 10) % 10;
	if (b >= 5){
    
    
		a + +;
	}

	else{
    
    
		a = a;
	}
	printf("%d", a);
}

Code running result:
Insert picture description here
problem-solving idea:
use numeric type conversion to force the floating-point data received by the keyboard into integer data, multiply the data by 10 and divide by 10 to get the remainder to get one digit after the decimal point, compare it with 5 The size judgment is rounded up or down.

Guess you like

Origin blog.csdn.net/qq_45621376/article/details/109392464