I have a function that involves floats and ints. When I run the code, the result comes out wrong. When I enter 4.3 for the input_voltage, I get 3803 for the decimal and EDB for HEX. The correct answer should be 3808 and EE0. Can anyone tell me what might be going on?
#include <stdio.h>
int digital_encoding(float voltage);
int main()
{
float input_voltage;
int valid, ch;
do{
printf("Please enter the input voltage between 0 and 5 volts: \n");
valid = scanf("%f", &input_voltage);
if(input_voltage <= 0){
printf("Enter a number larger than 0! \n");
valid = 0;
}
if(input_voltage >= 5){
printf("Enter a number less than 5! \n");
valid = 0;
}
}while(valid != 1);
digital_encoding(input_voltage);
}
int digital_encoding(float voltage)
{
int dig_encode;
dig_encode = ((voltage + 5)*(4095/10));
printf("The digital encoding equals %d in decimal, and %X in HEX.\n", dig_encode, dig_encode);
return dig_encode;
}
The problem lies here :
Use this instead