1. ## A simple program about displaying number

Hello everyone!

This is my first post here...sorry if someone already started a thread before..i couldnt find one related to this...

okay..i want to write a simple program that gets a floating point number from the user and display only the right most digit that is before the decimal point...

i have tried out one and found working too...

what i need to know is that..is there any other way to do this....a simple way ofcourse..

Here is my code...

Code:
```#include<stdio.h>
void main()
{
float x;
int i,temp,check;
clrscr();
printf("Enter a real number:");
scanf("%f",&x);
temp=x;
for(i=0;i<=9;i++)
{

check=temp+i;
if(check%10==0)
break;
else
check=check-i;

}
i=10-i;
printf("\n %d",i);
getch();
}```

btw i use win xp and turbo c version 3.0 from borland...

2. There is a simpler way.

--
Mats

3. Hello matsp...thanks for the fast reply...ok now u say theres an easier way to do this...but how?
can u throw some light over that please?

4. Well, the first step is that you make x into an integer by "temp = x", right? So if you have an integer, how can you find the lowest digit in it?

--
Mats

5. dear matsp i want to find out the unit digit not the ones in the fractional part....like for example in the number 23.743 the output has to show the number 3...for 4379.1834 it should display 9

the fractional part is not important....also the number should be entered in floating point format...
so i just left the compiler to make the floating number truncate its fractional part and stored in the variable 'temp' which is an integer..

thanks for ur inputs anyway

6. Originally Posted by sundar
dear matsp i want to find out the unit digit not the ones in the fractional part....like for example in the number 23.743 the output has to show the number 3...for 4379.1834 it should display 9

the fractional part is not important....also the number should be entered in floating point format...
so i just left the compiler to make the floating number truncate its fractional part and stored in the variable 'temp' which is an integer..

thanks for ur inputs anyway
Yes, I understand what you are trying to do. What is the result of temp % 10?

--
Mats

7. well i am doing modulo division here so that i can find whether the number is divisible by 10 or not...if it is, it would produce a zero remainder...

actually what i am doing is...i am going to add a single digit to the whole number..the digit can be anything between 0 to 9...each time when it adds a digit using the FOR loop...the IF condition checks if it is divisible by 10 or not...

if it does then it exits...and by subtracting 10 from it you will get the exact number which was there before....

i could get the right answer to many numbers....or am i wrong somewhere?

8. For each of these numbers:
241
682
755
300
figure out what the answer you want to get is (the right-most digit before the decimal point). Then figure out what temp%10 would be.

9. hello tabstop...thanks for the post...

just tried out the numbers....and i found a bug in it....thanks for letting me know that

when i tried the number 300 it displayed 10 while it should be 0

ok now ill use an IF condition at the end to solve this problem....and later ill post the code here...

10. Originally Posted by sundar
hello tabstop...thanks for the post...

just tried out the numbers....and i found a bug in it....thanks for letting me know that

when i tried the number 300 it displayed 10 while it should be 0

ok now ill use an IF condition at the end to solve this problem....and later ill post the code here...
You don't need another if-condition, you need to remove a whole lot of lines, and replace it with ONE. That is what both me and tabstop is trying to say.

--
Mats

11. Originally Posted by matsp
You don't need another if-condition, you need to remove a whole lot of lines, and replace it with ONE. That is what both me and tabstop is trying to say.

--
Mats
Oh darn, I was just about to post that it can be done in one line...

QuantumPete

12. sorry for my stupidity....actually i tried with an IF statement but now ive changed the loop from 1 to 9 and its working perfect....thank u everyone...now i want to know if theres any other solution...

13. Originally Posted by sundar
sorry for my stupidity....actually i tried with an IF statement but now ive changed the loop from 1 to 9 and its working perfect....thank u everyone...now i want to know if theres any other solution...
That's what we've been saying. There is. After the scanf, you need just *ONE* more line of code, that will print out the answer.

QuantumPete

14. what do u mean guys?

is there any keyword u want me to add?

or is it like something in the scanf function that can make me get the unit digit ?

thanks

15. Originally Posted by sundar
what do u mean guys?

is there any keyword u want me to add?

or is it like something in the scanf function that can make me get the unit digit ?

thanks
http://cboard.cprogramming.com/showp...98&postcount=6