Thread: how to calculate billion number

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Exclamation how to calculate billion number

    i want write program to calculate billion number but when i input a lot of number my program will show negative number

    this my code

    Code:
    int main()
    {
        double long a,b,c;
        
        scanf ("%d",&a);
        scanf ("%d",&b);
        c=a+b;
        printf("%d",c);
        
        
    getch();
        }
    why don't work?

    plz help me


    ps.sorry my eng is not strong.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because normal 32 bit integers (the most common) have a max positive value of 2147483647

    If you want something much bigger, tell us which compiler you're using.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Exclamation

    i use turbo c++

    thx for u help

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i use turbo c++
    Start by getting a newer compiler. Do you have any idea how old Turbo C++ is?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    i think it came out in '91

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Exactly. For starters, use a long instead of an int.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15
    int can only hold values of -32,768 through 32,767. Try compling this bit of code (Taken from SAMS Teach Yourself C++ In 24 Hours)
    Code:
    #include <iostream>
    
    int main()
    {
           int smallNumber;
           smallNumber = 32767;
           std::cout << "small number:" << smallNumber << std::endl;
           smallNumber++;
           std::cout << "small number" << smallNumber << std::endl;
           smallNumber++;
           std::cout << "small number" << smallNumber << std::endl;
           return 0;
    
    }
    When you see the output, you will notice that after I increment the number with ++, it wraps around to the smallest number it can hold, which is -32767. This is called (you guessed it) "Wrap Around." Use an unsigned long int for positive #s up to 4,294,967,295. Hope it helps, a fellow n00b programmer, Sintu

  8. #8
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    How about a 'long long int' or is this not standard.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  9. #9
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    long long isn't standard but it's reasonably well supported I think.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not on the OP's fossil compiler it isn't.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM