A simple program about displaying number

This is a discussion on A simple program about displaying number within the C Programming forums, part of the General Programming Boards category; Hello everyone! This is my first post here...sorry if someone already started a thread before..i couldnt find one related to ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    chennai
    Posts
    9

    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();
    }
    Thanks in advance for your inputs

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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is a simpler way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    chennai
    Posts
    9
    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. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    chennai
    Posts
    9
    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. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sundar View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    chennai
    Posts
    9
    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. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #9
    Registered User
    Join Date
    Aug 2008
    Location
    chennai
    Posts
    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. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sundar View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    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
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    Registered User
    Join Date
    Aug 2008
    Location
    chennai
    Posts
    9
    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. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by sundar View Post
    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
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #14
    Registered User
    Join Date
    Aug 2008
    Location
    chennai
    Posts
    9
    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. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by sundar View Post
    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
    A simple program about displaying number

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. Replies: 7
    Last Post: 08-19-2007, 09:10 AM
  3. Largest number program
    By rebel in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2005, 04:20 AM
  4. Mystery bug in simple program? - I am beginner
    By archie123 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 08:23 AM
  5. Replies: 4
    Last Post: 03-09-2002, 01:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21