445,645 Members | 1,078 Online Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

# program that computes and displays the sum of all integers...

 P: 12 Im doing a program for one of my classes and im no good. here is the question Write a program that computes and displays the sum of all integers from 1 to an ENDING number, using a while loop. Prompt the user for the ending number. For example, if the user enters 99, then the program will compute the sum of all the integers from 1 to 99 and would display the final sum of 4950. Im no sure how to do the while loop and add the intergers. If some can show me how to write this program then i just might understand it. All help is appreciated. Sep 23 '06 #1
3 Replies

 Expert Mod 5K+ P: 8,916 While loops go like Expand|Select|Wrap|Line Numbers int ix = 0;   while(ix<50) {    printf("ix = %d\n", ix); }   adding integers is Expand|Select|Wrap|Line Numbers int a, b, c;   a = 5; b = 6; c = 0;   printf("A=%d, b=%d, c=%d\n", a, b, c);   a++;   printf("A=%d, b=%d, c=%d\n", a, b, c);   a += b;   printf("A=%d, b=%d, c=%d\n", a, b, c);   c = a + b;   printf("A=%d, b=%d, c=%d\n", a, b, c);     Sep 23 '06 #2

 P: 12 This is what i have so far, its rough but does have an output [ #include #include using namespace std; int main() { int I; int sum = 0; int n = 1; cout <<"Enter Positive Interger" << endl; cin >> I; //Add all intergers from 1 to I to sum while (n <= I) { //add the sum and n together, and assign this new value to verible sum sum = sum + n; n++; //Display the sum of the added intergers cout << "The Total is " << sum << endl; system("PAUSE"); return EXIT_SUCCESS; } } ] Sep 23 '06 #3

 Expert Mod 5K+ P: 8,916 You have a brace inthe wrong place. This would be obvious if you indented you code correctly Expand|Select|Wrap|Line Numbers #include  #include    using namespace std;   int main() {     int I;     int sum = 0;     int n = 1;     cout <<"Enter Positive Interger" << endl;     cin >> I;     //Add all intergers from 1 to I to sum     while (n <= I)     {         //add the sum and n together, and assign this new value to verible sum         sum = sum + n;         n++;         //Display the sum of the added intergers         cout << "The Total is "  << sum << endl;         system("PAUSE");         return EXIT_SUCCESS;     } }   You have cout << "The Total is " << sum << endl; system("PAUSE"); return EXIT_SUCCESS; inside the while loop this means the program outputs the result, "PAUSE"s and returns on the first itteration of the while loop. These statements should come after the while loop. Sep 24 '06 #4 