Thread: while looping

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    Question while looping

    I am trying to figure out how to make a program so that when I enter a starting number and an ending number all numbers between that are added up.

    For Example
    First Number: 1
    Second Number: 5

    The program will: 1+2+3+4+5 = answer

    This is what I have so far

    #include<iostream.h>
    #include<math.h>

    main()
    {
    int firstnum, secondnum, x, answer;

    answer = 0;

    cout<<"Please enter beginning term: ";
    cin>>firstnum;
    cout<<"Please enter ending term: ";
    cin>>secondnum;


    while(firstnum<=secondnum)
    {
    answer = firstnum++;
    {
    cout<<answer<<endl;
    }
    } return 0;
    }
    This prints
    1
    2
    3
    4
    5

    How would I get it to 1 + 2 + 3 + 4 + 5 using the while loop?

    Any help would be appreciated.

    Thank You

    [email protected]

  2. #2
    g
    Guest
    using a for loop it would be

    int answer = 0;
    for(i = startingNumberp; i <= endingNumber; i++)
    {
    answer += i; //expands to answer = answer + i;
    }
    cout << answer;

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    This should do the trick.
    Code:
    answer = 0;
    
    while(firstnum <= lastnum)
         answer += firstnum++;
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    maybe this will work,i didn't test it though, it may be wrong.


    int total=firstnum;
    while(firstnum<=secondnum) {
    cout<<answer<<" + ";
    answer = firstnum++;
    total+=answer;
    }
    cout<<" = "<<total;

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    Question still trying to figure out while loop

    I tried correcting the while loop... I am a step closer but not quite there....

    The code I now am using is

    #include<iostream.h>
    #include<math.h>

    main()
    {
    int firstnum, secondnum, answer;


    cout<<"Please enter beginning term: ";
    cin>>firstnum;
    cout<<"Please enter ending term: ";
    cin>>secondnum;

    int total = firstnum;
    while(firstnum<=secondnum)
    {
    cout<<answer<<" + ";

    answer = firstnum++;
    total +=answer;
    }
    cout<<" = "<<total;
    }

    When I run the program I get..

    Please enter beginning term: 1 // i entered the number 1
    Please enter ending term: 5 // i entered the number 5
    5368726784 + 1 + 2 + 3 + 4 + = 16

    I'm not sure why I am getting 5368726784.. any suggestions would be appreciated...

    when the program is correct it should say 1 + 2 + 3 + 4 + 5 = 15

    I apprecate the help so far... Any more would be even more appreciated.

    Thank You

    [email protected]

  6. #6
    Unregistered
    Guest

    use this idea for the loop

    int total=0
    int temp=firstnum;
    while(firstnum<=lastnum)
    {
    cout<<temp<<"+";
    total=(total+temp);
    temp++;
    }
    cout<<"="<<total;

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    Unhappy still need help Please

    I tried the last suggestion unfortuantely I it was never ending... information with I believe six digits plus six digits just kept continuing... this is the code I used.

    #include<iostream.h>
    #include<math.h>

    main()
    {
    int firstnum, temp, secondnum, answer;
    int total = 0;

    cout<<"Please enter beginning term: ";
    cin>>firstnum;
    cout<<"Please enter ending term: ";
    cin>>secondnum;

    temp = firstnum;
    while(firstnum<=secondnum)
    {
    cout<<temp<<" + ";
    total=(total+temp);
    temp++;
    }
    cout<<" = "<<total;
    }

    Any additional help would be appeciated... I still have some hope that I will end up being able to enjoy a little of the weekend.

    Thank You

    [email protected]

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    this should work:

    main()
    {
    int firstnum, secondnum, answer;
    int total = 0;

    cout<<"Please enter beginning term: ";
    cin>>firstnum;
    cout<<"Please enter ending term: ";
    cin>>secondnum;

    while(firstnum<=secondnum)
    {
    cout<<firstnum<<" + ";
    total=(total+firstnum);
    firstnum++;
    }
    cout<<" = "<<total;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM