Thread: Math is wrong but still an accurate output???

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    4

    Math is wrong but still an accurate output???

    I'm trying to follow the math of this example program but as you can see in the comments I've been trying to work out the math, but when I follow the given equations it doesn't calculate correctly, yet the program still gives an accurate output...what am I missing? How is the change function working?

    Code:
    #include <stdio.h>
    
    
    
    #include<stdio.h>
    void change(float coin)
    {
        float abc = coin*10.0*10.0; /*12.59 * 10.0 * 10.0=1259*/
        int sum = abc;
        int x, y, z;
        
    
        z = sum / 25;  /*   1259/25= 50.36  */
        printf(" %d quarters \n", z);
        sum = sum - z * 25; /*1259-50.36*25*/
        y = sum / 10;
        printf(" %d dime \n", y);
        sum = sum - y * 10;
        x = sum / 5;
        printf(" %d nickels \n", x);
        sum = sum - x * 5;
        printf(" %d pennies \n", sum);
    }
    int main()
    {
        float d;
        
        printf(" Enter a value for change :");/*EXAMPLE TEST WITH $12.59*/
        scanf_s("%f", &d);
        change(d);
    
        getchar();
        getchar();
        return 0;
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Lookup integer division in C.

    1259/25 = 50

    Edit: Remember 3/4 = 0

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if else wrong little bit include math
    By Ph0x in forum C Programming
    Replies: 5
    Last Post: 09-23-2014, 03:10 PM
  2. math being done wrong?
    By Cynder in forum C Programming
    Replies: 4
    Last Post: 04-24-2012, 04:20 PM
  3. Help! Right Math, Wrong Output
    By verd in forum C Programming
    Replies: 12
    Last Post: 03-15-2005, 07:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. pointer int math wrong
    By mart_man00 in forum C Programming
    Replies: 6
    Last Post: 04-26-2003, 09:17 PM

Tags for this Thread