Thread: need help separating a real number

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    19

    need help separating a real number

    ok

    its for a programming C class not sure how to do this so whoever can lend a hand i'll appreciate it. here is the assignment.


    Write a program that takes a real number as input and prints out the
    integer and fractional part.

    Example: If the input is 16.343, the output should say:

    The integer part is 16 and the fractional part is 0.343.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The power of casting! Assuming your CPU/Compiler truncates instead of rounds (AFAIK some compilers give the option to round).

    Then,

    Code:
    float real = 16.343;
    int integerPart = (int) real;
    float fractionalPart = real - (float)integerPart;
    Or something, there are many other ways to do it though...

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    float fractionalPart = real - (float)integerPart;
    The cast is not needed, as real is a float already, and that will promote both sides to float.

    I'm 99.9% sure that this will work on all compilers that follow the C standard.

    --
    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.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    I'm 99.9% sure that this will work on all compilers that follow the C standard.
    And according to Sod's Law, your lecturer will have a compiler that falls into the 0.1% bracket.

    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  3. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  4. Real Number Representation
    By foxblubba in forum C Programming
    Replies: 1
    Last Post: 06-08-2002, 05:52 AM
  5. How can I get a REAL RANDOM number?
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-04-2001, 01:22 AM