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.
3 15469 Banfa 9,065
Expert Mod 8TB
While loops go like -
int ix = 0;
-
-
while(ix<50)
-
{
-
printf("ix = %d\n", ix);
-
}
-
adding integers is -
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);
-
-
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;
}
}
]
Banfa 9,065
Expert Mod 8TB
You have a brace inthe wrong place. This would be obvious if you indented you code correctly -
#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;
-
}
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
| |