0

Hello, I am new to C and not really sure how to do this. This program extracts and prints the rightmost digit of the integer portion of a float, and I need to convert it but I'm not sure how.

#include <stdio.h>

void main (void)
{

 float num;
 
 int rightdigit;

 printf("Enter an integral number: ");
 scanf("%d", &num);

 rightdigit = num % 10;
 printf("\nThe right digit is: %d", rightdigit);

}
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Shankye
1

First of all, when you scanning float u should use %f

scanf("%f", &num);

I think u need an int operand to to use with %..
so cast the float to int with (int)

rightdigit = (int)num % 10;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.