print only last 3 number float in c

Printing the Last 3 Numbers of a Float in C

To print only the last 3 numbers of a float in C, you can use the printf function along with format specifiers. Here's an example code snippet that demonstrates this:

#include <stdio.h>

int main() {
    float number = 1234.5678;
    int lastThreeDigits = (int)(number * 1000) % 1000;
    printf("Last three digits: %03d\n", lastThreeDigits);
    return 0;
}

Explanation of the code: 1. We declare a float variable number and assign it a value of 1234.5678. 2. We calculate the last three digits of the float by multiplying the float by 1000 and then taking the remainder when divided by 1000. This is done using the expression (int)(number * 1000) % 1000. 3. We use the printf function to print the last three digits. The format specifier %03d ensures that the number is printed with leading zeros if it has less than three digits.

The output of the above code will be:

Last three digits: 567

Please note that this code assumes that the float number has at least three decimal places. If the float number has fewer than three decimal places, the output will include leading zeros.