Like Tree4Likes

Why the output of my program is "0" when I multiply large numbers like 256 and 8192 i

This is a discussion on Why the output of my program is "0" when I multiply large numbers like 256 and 8192 i within the C Programming forums, part of the General Programming Boards category; Hi. Could someone help me. I am making a C language program that will display the product of any large ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    Unhappy Why the output of my program is "0" when I multiply large numbers like 256 and 8192

    Hi. Could someone help me. I am making a C language program that will display the product of any large number, but I a getting 0 instead! Here is a sample program that I made...

    Source code:
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main();
    {
    long long int a=256;
    long long int b=8192;
    long long int ans=a*b;
    printf("%d",ans);
    getch();
    return 0;
    }
    Output: 0

    We all know that 256 x 8192 = 2097152, but why the computer is still displays "0"? I tried changing ans to double, but still it is "0". What will I do?

    PS. If you will make a program modifying it, please make it as simple as possible. Thanks.
    Last edited by Nagasaki; 08-14-2011 at 01:53 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Well you need to look up the correct format letter(s) for printing a long long.

    If you're using a port of MinGW (dev-c++, code::blocks) then you need to check how to use printf on the MSDN site.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Quote Originally Posted by Salem View Post
    Well you need to look up the correct format letter(s) for printing a long long.

    If you're using a port of MinGW (dev-c++, code::blocks) then you need to check how to use printf on the MSDN site.
    So how can I display the right value? By the way, I am using the Turbo C in DOS.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Nagasaki View Post
    So how can I display the right value? By the way, I am using the Turbo C in DOS.
    Salem will be DELIGHTED to hear that! < rofl >

    Have you tried using %lld as the printf format?

    The right format is right there in your help for printf().

    There are limits to the size of any data type. You may want to look up your system's values. They can be found in limits.h and ctype.h, both (in my version of Turbo C).
    Last edited by Adak; 08-14-2011 at 03:28 AM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    ok, I got it. All I need is change %d to %ld. I just want to ask something. If %ld can display a 7-digit number, How I can print a 11-digit number?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Turbo C was set to the AT&T standard, in it's earliest versions, and had only unsigned long int as it's highest integral data type. I'm not sure what the later versions of Turbo C had.

    For a larger number, you could:

    1) Check out limits.h and see what your largest double is. Use the double if it's big enough, and then round off the double, and print only the part to the left of the decimal point.

    2) Use an array of digits (char or int), to hold your big int's. You'll have to write up your own functions to handle addition, subtraction, multiplication and division, and anything else you want. Not too bad for the basics, but if you want higher math functions, use #3.

    3) Get a large number library. GNU has one that's well known. Laserlight linked to it just last week, here.

    Using an unsigned int will effectively double the range of your positive int's, but eleven digits is a reach.
    Nagasaki likes this.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Oh. That's a big help. I am actually using this sample program as a reference to my real program. Our professor wants us to make a program that will output this using the function call.
    1 2 2 4 8 32 256 8192 2097152 17179869184
    I have made a program, but as of now, I got the 1st to the 9th element, my problem is on the 10th and last element. Can a 2-dimensional long array do it?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Nagasaki View Post
    So how can I display the right value? By the way, I am using the Turbo C in DOS.
    Fred Flintstone will be so proud!

    1) Get a better compiler... that one is at least 20 years out of date.

    2) Get a better operating system ... nobody has actualy used MS-DOS in over a decade.

    3) Get a better school ... any school still teaching Turbo C or DOS is so behind the times your career is doomed to failure before it even starts.

    4) Click the red link in my .sig (bottom of this message)... read and believe!

    Pelles C <-- 32 and 64 bit compilers, full windows header and lib set, full set of resource editors, best help file available ... and it's free.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So you may be able to use the #1 technique then. That would be a lot easier than anything else.

    To make it work declare your variables as doubles, and do the exponentiation of 2, as you need to (21 times I believe). Your answer might be OK, because it's a power of 2. If it needs to be rounded, add 0.5 to it and then divide by 1, to round it off.

    Give that a shot.

    I'm getting that sneaky feeling that there is a bit based, power of 2, super easy answer to this, but I don't know it. Did your prof mention something like this, and like to be "sneaky"?
    Last edited by Adak; 08-14-2011 at 10:39 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Quote Originally Posted by Adak
    I'm getting that sneaky feeling that there is a bit based, power of 2, super easy answer to this, but I don't know it.
    Well, you could add powers (and end up with the Fibonacci sequence) and then use a left shift, but keeping track of the last two numbers generated would work just as well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The problem with 256 * 8192 is that it's too big for a 16 bit integer and since it is powers of 2, the result left in the lower 16 bits is all 0s.

    Do yourself a favor...
    Code:
    #include <stdio.h>
    
    int main (void)
     { 
        printf("%d\n", sizeof (int));
        printf("%d\n", sizeof (long int));
        printf("%d\n", sizeof (long long int));
        return 0;
      }
    If your answers are 2 4 4 instead of 4 4 8 .... you need a new compiler.

    The biggest number you're going to put into 2 bytes is 65,535
    The biggest number for 4 bytes is 4,294,967,295
    The bittest number for 8 bytes is 18,446,744,073,709,551,614

    Can you see why a new compiler might be a good idea?

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I didn't ask him how he was finding the next number in the sequence. He said he's finding them, so I took that to mean he had the math logic working OK.

    I would say it's a bit odd that all the numbers are powers of two, but then CommonTater or Andrew would just point out that no, it was EVEN - so I won't.
    AndrewHunter likes this.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Adak View Post
    I would say it's a bit odd that all the numbers are powers of two
    Only if you don't notice what the pattern actually is. But yes, bit shifting is only going to work if he's got 21 bits there for him to shift into; shifting a 1 off the edge of the register and down to the bottom of the computer case doesn't help.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Only if you don't notice what the pattern actually is. But yes, bit shifting is only going to work if he's got 21 bits there for him to shift into; shifting a 1 off the edge of the register and down to the bottom of the computer case doesn't help.
    Oh WOW... after all these years... I finally know where all that dust in my computer cases comes from!

    THANK YOU!

    LOL...

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiply arbitrarily large real numbers
    By Martin0027 in forum C Programming
    Replies: 7
    Last Post: 05-17-2011, 01:59 PM
  2. Replies: 2
    Last Post: 01-30-2008, 05:12 PM
  3. How to use "switch" to perform large numbers cases?
    By megablue in forum C Programming
    Replies: 4
    Last Post: 07-09-2003, 10:51 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21