Thread: HELP with a C/C++ program adding numbers...

  1. #16
    Registered User
    Join Date
    Nov 2001
    Posts
    14
    If you use the long int, it gives a range of about 4 million, but that is not enough if the numbers from 1 to a million are added, and the sum is 50000005000000 or something like that...I looked up on the help manual and you can also use long double, but I don't know how to use that...I also don't know how to use the printf function with long int and long double...

  2. #17
    Unregistered
    Guest
    trya more limited program first, say adding the each number 1 -10 to each other:

    #include <stdio.h>

    int main()
    {
    int a, b;
    a = 0;
    b = 0;
    while (a<10)
    {
    a++;
    b = a + b;
    printf("%d\n, a)
    }
    return 0;
    }


    If that works then try increasing the value in the while conditional until it breaks the system. That will give you some useful experience.

    Then try changing the data type for variable a to double instead of int and continue increasing the value in the while loop. That should give you additional valuable experience. Since I don't know if d is the appropriate manipulator to use for type double when formatting output with printf, I would add the line #include <iostream.h> and change the printf() to simply cout << a.

    When the system stops working with type double then change a to type long double. I believe you are supposed to be able to handle numbers with up to 19 digits with type long double. Otherwise you could also try changing a to type float and use scientific notation.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    int main() 
    { 
    unsigned long a = 0;
    unsigned long b = 0; 
    while (a<1000001UL) 
    {a++; 
    b=a+b; 
    printf("%lu\n", a); //or %ld for signed longs
    } 
    printf("%lu\n", b); //or %ld for signed longs
    return 0; 
    }
    I'm not sure if the 1000001UL in needed (UL stands for unsigned long). See if if makes a difference.
    If your numbers are never negative, then use %lu. If numbers are negative and positive, use %ld.
    For long double use %lf in printf().
    Last edited by swoopy; 11-16-2001 at 01:52 PM.

  4. #19
    Registered User
    Join Date
    Nov 2001
    Posts
    14

    Unhappy

    Swoopy, I used the program above and there are several errors...
    line 12 statement missing ; in function main()
    line 16 compound statement missing } in function main()
    line 16 function should return a value in function main()

  5. #20
    Registered User
    Join Date
    Nov 2001
    Posts
    14

    Unhappy

    When I use lf for the printf statement for long doubles, it returns a bunch of jibberish...

  6. #21
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Your errors sound like you made typos. Try this. If any errors pop up, check the spelling very carefully.

    PHP Code:

    #include <stdio.h>

    int main() 

        
    unsigned long a 0;
        
    unsigned long b 0
        
        while( 
    1000001UL 
        {
            
    a++; 

            
    b;
            
            
    printf("%lu\n"a); //or %ld for signed longs
        
    }
        
        
    printf("%lu\n"b); //or %ld for signed longs

        
    return 0

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #22
    Registered User
    Join Date
    Nov 2001
    Posts
    14

    Talking

    OK....thanks everybody, this program works now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Need HELP with Program
    By Jeff in forum C Programming
    Replies: 25
    Last Post: 09-23-2004, 08:03 PM
  5. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:03 PM