write a program in c that reads a floating point number and then displays the right most…
- User
write a program in c that reads a floating point number and then displays the right most digit of the integral part of the number
- Assistant
write a program in c that reads a floating point number and then displays the right most digit of the integral part of the number
Certainly! Below is a C program that reads a floating-point number from the user, extracts the integral part of the number, and then displays the rightmost digit of that integral part. The program handles both positive and negative numbers and ensures that edge cases (such as numbers with an integral part of zero) are properly managed.
#include <stdio.h>
#include <math.h>
int main() {
double num;
double int_part;
int last_digit;
// Prompt the user to enter a floating-point number
printf("Enter a floating-point number: ");
if (scanf("%lf", &num) != 1) {
printf("Invalid input. Please enter a valid floating-point number.\n");
return 1;
}
// Extract the integral part of the number
int_part = trunc(num); // trunc() removes the fractional part
// Convert the integral part to a long integer to handle large numbers
long long integral = (long long)int_part;
// Handle negative numbers by taking the absolute value
if (integral < 0) {
integral = -integral;
}
// If the integral part is 0, the rightmost digit is 0
if (integral == 0) {
last_digit = 0;
} else {
// Obtain the rightmost digit using modulo operator
last_digit = integral % 10;
}
// Display the rightmost digit
printf("The rightmost digit of the integral part is: %d\n", last_digit);
return 0;
}
Including Headers:
stdio.h is included for input and output functions.math.h is included for the trunc() function, which truncates the decimal part of a floating-point number.Reading Input:
scanf with the %lf format specifier to read a double precision floating-point number. If the input is invalid, the program alerts the user and exits.Extracting the Integral Part:
trunc(num) is used to obtain the integral part of the number by removing the fractional part. This function works correctly for both positive and negative numbers.Handling Large Numbers and Negative Values:
long long type to accommodate large integers beyond the range of a standard int.Determining the Rightmost Digit:
0, the rightmost digit is naturally 0.% 10 is used to find the last digit of the integral part.Displaying the Result:
Example 1:
Enter a floating-point number: 123.456
The rightmost digit of the integral part is: 3
Example 2:
Enter a floating-point number: -789.012
The rightmost digit of the integral part is: 9
Example 3:
Enter a floating-point number: 0.99
The rightmost digit of the integral part is: 0
The program uses trunc() instead of typecasting to ensure that it correctly handles cases where the floating-point number is negative. Simple typecasting (e.g., (long long)num) might not always behave as expected with negative numbers because it truncates towards zero, which trunc() also does, but using trunc() makes the intention clearer.
The use of long long ensures that the program can handle very large integral parts without overflow, but if you are certain that the input will be within the range of a standard int, you can simplify the code by using int instead.
Always ensure that the user inputs a valid floating-point number to prevent undefined behavior or incorrect results.
I hope this helps! Let me know if you have any questions or need further assistance.
Not yet included in a published release.