Hey guys, i need some help with 2 programs that my teacher assigned me..

I just cant seem to figure them out. I'll type out what the program is, but I really dont have much code to show since I dont really know how to even get started so any help would be welcomed..

The only part I really dont know how to do is the average part.

--------------------------------

1. Write a program that asks the usr for a positive integer value. The program should use a "while-loop" to get the sum of all the integers from 1 to the number entered.

3
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by Ancient Dragon

1. Write a program that asks the usr for a positive integer value. The program should use a "while-loop" to get the sum of all the integers from 1 to the number entered.

post some code....

where did you get stuck?
tip:: you read a number and then you use it as as an "end" condition in a for loop

Here's all I got.. I just don't know how to find the sum of the numbers between 1 and the number they entered.. :\

``````#include <iostream>
using namespace std;

int main()
{
int num = 1;
int picknum = 0;

cout << "Chose a positive number." << endl;
cin >> picknum;

while (num ???)
{
cout << "The sum of the number 1 to your desired number = " << (num``````

>>I just don't know how to find the sum of the numbers between 1 and the number they entered

How would you do it yourself with pencil & paper? If I enter 5 then you want 1+2+3+4+5. Now do the same thing with a loop and two counters -- one counter is a loop counter and the other variable is the sum of the value of the loop counter on each iteration of the loop.

Would I use a count-controlled loop? I'm still trying to figure out how to use the loops and counters since I just learned about them in class so sorry for the newbie questions.

Your instructions say you must use a while loop. You will need two integers, one to count from 1 to whatever number you enter, and the other to sum up the values. Do it a little bit at a time so that you don't get overwhelmed. First how to create that while loop. After you get that done then add the counter to count from 1 to N. Only after that works do you worry about the sum integer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.