Member Avatar for CanofCornInfini

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);

}

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.