Thread: Problem with multiplication

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Problem with multiplication

    Why don't I get the same result if I multiply all together in the first example?
    The second gives the good result.

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    int formatThousands(long long value)
    {
        string num = to_string(value);
        int insertPosition = num.length() - 3;
        while (insertPosition > 0)
        {
            num.insert(insertPosition, " ");
            insertPosition-=3;
        }
        cout << num << endl;
        return 0;
    }
    
    
    int main()
    {
        // 1.
        long long int chance = 0;
        chance += 49*48*47*46*45*44;
        cout << " Chance: ";
        formatThousands(chance);
        cout << "\n\n";
    
        //2.
        chance=0;
        chance += (49*48*47*46*45);
        chance *= 44;
        cout << " Chance: ";
        formatThousands(chance);
        cout << "\n\n";
    
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Need to declare the constants as long long, as the product is > 32 bits.

    Code:
        chance += 49ll*48ll*47ll*46ll*45ll*44ll;
    or just casting any of them should promote all to long long:

    Code:
        chance += (long long)49*48*47*46*45*44;
    Last edited by rcgldr; 12-05-2015 at 07:31 AM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ah, OK, thanks for the explanation!
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rcgldr View Post
    or just casting any of them should promote all to long long:
    Or you can specify that one of the constants is a long long, e.g. 49ll*48*47*46*45*44.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with 'Matrix Multiplication using Pointers'
    By JennyG in forum C Programming
    Replies: 20
    Last Post: 07-30-2012, 12:58 PM
  2. Hi, problem with pointers and matrix multiplication
    By Phadelity in forum C Programming
    Replies: 2
    Last Post: 11-23-2010, 11:53 PM
  3. HELP: Series of Multiplication Problem...
    By Ocin101 in forum C Programming
    Replies: 13
    Last Post: 07-20-2009, 07:38 AM
  4. Multiplication problem
    By Kempelen in forum C Programming
    Replies: 19
    Last Post: 05-21-2008, 01:56 PM
  5. Multiplication problem
    By awkeller in forum C Programming
    Replies: 4
    Last Post: 10-17-2001, 11:34 PM