Thread: long long?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Spain
    Posts
    5

    long long?

    Hi all ..
    I'm trying to do this:

    1111 x 1111 = 1234321
    11111 x 11111 = 123454321
    111111 x 111111 = 12345654321
    1111111 x 1111111 = 1234567654321
    11111111 x 11111111 = 123456787654321
    111111111 x 111111111=12345678987654321



    I wrote this code:
    Code:
    #include <stdio.h>
    
    int i;
    long long int product;
    int factor;
    
    int main(){
        factor = factor * 10 + 1;
        for(i=0;i<8;i++){
            factor = factor * 10 + 1;
            product = factor * factor;
            printf("%d x %d = %lld \n",factor, factor, product);
        }    
    
            
    }
    But I get this:


    11 x 11 = 121
    111 x 111 = 12321
    1111 x 1111 = 1234321
    11111 x 11111 = 123454321
    111111 x 111111 = -539247567
    1111111 x 1111111 = 1912040369
    11111111 x 11111111 = -2047269199
    111111111 x 111111111 = 1653732529

    Some help please? Thanks. :-)
    Last edited by Daniel Samper; 11-12-2011 at 04:46 PM.

  2. #2
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    Hi, I am new at C. But I wanna help. I got what you want. But in a "not useful" way. Sorry for this, I hope the experts() will help you but that is my solution.


    Code:
    #include <stdio.h>
     
    int i;
    long int product;
    int factor;
     
    int main(){
        factor = factor * 10 + 1;
    factor = factor * 10 + 1;
    factor = factor * 10 + 1;
        for(i=0;i<6;i++){
            factor = factor * 10 + 1;
            product = factor * factor;
            printf("%d x %d = %lld \n",factor, factor, product);
        }    
     
             
    }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Change the type of factor to be long long, not int.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Spain
    Posts
    5
    I wrote this:

    Code:
    #include <stdio.h>
    
    int i;
    long long product;
    int factor;
    
    int main(){
        factor = factor * 10 + 1;
        for(i=0;i<8;i++){
            factor = factor * 10 + 1;
            product = factor * factor;
            printf("%d x %d = %lld \n",factor, factor, product);
        }    
    
            
    }
    but I get the same screen ...

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Both factor and product must be either unsigned long long or long long.

    Jim
    Last edited by jimblumberg; 11-13-2011 at 09:07 AM.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    26
    Here is an article about numeric types in C that might help you:

    http://www.exforsys.com/tutorials/c-language/c-programming-language-data-types.html

    Y
    ou may want to try an unsigned long long int if you're confident that your integers will always be positive.

    As the tutorial explains, the size that you are actually allocated for numeric types is system dependent (although certain minimum values are guaranteed by the C standard). So, if you're pushing the upper limits of your type, you won't be guaranteed that it will work on all systems.

    If you want an arbitrarily large number and only need addition and multiplication, it might be interesting to write a function for adding and multiplying integers represented as strings. Maybe not this simplest solution but it could definitely be done and would free you from any built in size limitations.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Spain
    Posts
    5
    Thank you very much.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Beware of that page, there is a bunch of errors. Sloppy code.
    See my comment on that page.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. number bigger than long long double
    By suryak in forum C Programming
    Replies: 9
    Last Post: 08-18-2011, 02:02 PM
  2. Replies: 1
    Last Post: 04-23-2011, 08:40 PM
  3. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  4. Conver long to 8 byte array and back to long
    By plopes in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 12:39 AM
  5. STLport with MingW - Long Long error on project build
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 08-21-2006, 08:55 AM