473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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 15469
Banfa
9,065 Expert Mod 8TB
While loops go like

Expand|Select|Wrap|Line Numbers
  1. int ix = 0;
  2.  
  3. while(ix<50)
  4. {
  5.    printf("ix = %d\n", ix);
  6. }
  7.  
adding integers is

Expand|Select|Wrap|Line Numbers
  1. int a, b, c;
  2.  
  3. a = 5;
  4. b = 6;
  5. c = 0;
  6.  
  7. printf("A=%d, b=%d, c=%d\n", a, b, c);
  8.  
  9. a++;
  10.  
  11. printf("A=%d, b=%d, c=%d\n", a, b, c);
  12.  
  13. a += b;
  14.  
  15. printf("A=%d, b=%d, c=%d\n", a, b, c);
  16.  
  17. c = a + b;
  18.  
  19. printf("A=%d, b=%d, c=%d\n", a, b, c);
  20.  
  21.  
Sep 23 '06 #2
This is what i have so far, its rough but does have an output
[
#include <cstdlib>
#include <iostream>

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
Banfa
9,065 Expert Mod 8TB
You have a brace inthe wrong place. This would be obvious if you indented you code correctly

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int I;
  9.     int sum = 0;
  10.     int n = 1;
  11.     cout <<"Enter Positive Interger" << endl;
  12.     cin >> I;
  13.     //Add all intergers from 1 to I to sum
  14.     while (n <= I)
  15.     {
  16.         //add the sum and n together, and assign this new value to verible sum
  17.         sum = sum + n;
  18.         n++;
  19.         //Display the sum of the added intergers
  20.         cout << "The Total is "  << sum << endl;
  21.         system("PAUSE");
  22.         return EXIT_SUCCESS;
  23.     }
  24. }
  25.  
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

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd...
1
by: punkybrewster | last post by:
Basically my task is to write a program that computes and displays the weekly salaries of a company's salespeople based upon information entered by the user and that also displays the total for the...
0
by: joestevens232 | last post by:
I am seriously stuck and have been working on this for hours and hours and can't figure out my next step....heres the program assignment. This vending machine dispenses 1. M&Ms ($.65), 2. Chips...
14
by: MabZiCLe | last post by:
Hey guys, can you help me with this? C programming major in Turbo c. LOL Well, this is my problem. Create a program that accepts 10 positive integers. The program will group and sort all even...
6
by: phkram | last post by:
Hello everybody... i find this place very helpful. anyone who can help me solve the following problems? Thank you very much for helping me.. 1. Build a header file that consists of a class...
59
by: Umesh | last post by:
i wrote the following program to calculate factorial: #include<stdio.h> #include<iostream.h> void main() { int i,n; long int p=1; // or long double p=1; for exponential result which I don't...
4
by: Break2 | last post by:
I am trying to write a simple program which asks a user to enter 5 integers after which the average will be computed and written to the screen. That simple. However I want to do it according to a...
1
by: soumya0406 | last post by:
Can anyone help me in doing this program.I am new in the field of c and unix. I need to submit it in my college. I). A set of "slaves" processes: Each slave process gets two small integers from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.