Results 1 to 6 of 6

Thread: Problem of subtraction in C

  1. #1
    Join Date
    Dec 2009
    Posts
    80

    Problem of subtraction in C

    Hello friends i have started to learn C programming and i am very much familiar about the data types like integer also know a little about floating point variables. But the floating point is troubling me. I have made a simple program for subtracting two numbers but it is troubling me because the output what i get has six digits after the decimal point so please help me.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Problem of subtraction in C

    To be more precise on your problem if you are can post your program than i can get the clear picture of your problem or just give me some more details about the programs else state the data type or the function you are using in the program. But its good if you post the whole program. Hope you will post it.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Problem of subtraction in C

    If you are facing a problem in the floating point variable then let me tell you one thing that this is the easiest stuff in the C language. Try to use the %.2f in your program. This will truncate number of digits appearing after the the decimal to only two numbers for example if you are getting the output as 100.000000 then you will get the new output to 100.00 .

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Problem of subtraction in C

    This is very simple because you just have to use the %2f in your program. The output you are getting will automatically show only 2 digits after the decimal point . So please use this in built function it is as simple as that, Similarly if you want three numbers after the decimal point use %3f and %4f as you require.

  5. #5
    Join Date
    Dec 2009
    Posts
    80

    Re: Problem of subtraction in C

    As some of you guys had asked me to explain the programs in more detail so i am trying to give you some segments of the program.
    Consider:
    I am having to numbers say it as n1, n2 which are declared as float
    float n1= 50.50
    float n2=100
    Now, When i subtract
    float n3=n2-n1
    i am getting :49.500005
    but i want it as simply 49.50

    I think that my problem is somewhat clear now so please help me

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Problem of subtraction in C

    Hi, I got your point and will definitely help you i think you are not aware of the % operator in C You just have to use this operator while you are try to display the decimal digits in a c program.
    Just change the out put statement similar to the one below:
    printf("The subtarction of the two number is equal to: %2f" ,n3);

Similar Threads

  1. automatic subtraction in excel
    By smartsoul2 in forum Windows Software
    Replies: 3
    Last Post: 10-02-2011, 05:58 PM
  2. Replies: 6
    Last Post: 27-07-2010, 12:10 AM
  3. Replies: 4
    Last Post: 10-04-2010, 04:19 PM
  4. Problem in random number subtraction
    By Linoo in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 07:37 PM
  5. Subtraction between two SQL tables
    By KADRI in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 05:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,494,726.20581 seconds with 16 queries