dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: math

  1. #1
    Join Date
    Sep 2008
    Posts
    10

    math

    so im tryin to write this program which inputs 2 integer numbers (a and b), then outputs the sum (a+b) of those numbers, the difference (a-b) of those numbers, the product (a*b) of those numbers, and the ratio (a/b) of those numbers.

    as to now heres what i have i was wondering how to go about division

    // math
    #include <iostream>
    using namespace std;

    int addition (int a, int b)
    {
    int r;
    r=a+b;
    return (r);
    }

    int main ()
    {
    int z;
    z = addition (5,3);
    cout << "The result is " << z;
    return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,559

    Re: math

    return a / b;

    Keep in mind that integer division truncates down to the nearest int.
    1 / 2 = 0
    2 / 2 = 1
    3 / 2 = 1 etc.

    Use float or double types if you want to avoid truncation like that.

    Also, your addition function really only needs one line.

    return a + b;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)