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.

Dani AI

Generated

Quick, practical plan that ties together the prior tips from and : read and validate a positive integer, keep two variables (an accumulator and a loop counter), then run a while-loop that adds the counter into the accumulator and increments the counter until it passes the chosen number. The teacher wants a while-loop, so implement the loop rather than switching to a for-loop.

A concise, working example (uses 64-bit integers and basic input validation):

#include <iostream>

int main() {
    std::cout << "Enter a positive integer: ";
    long long N;
    if (!(std::cin >> N) || N <= 0) {
        std::cerr << "Invalid input\n";
        return 1;
    }

    long long sum = 0;
    long long i = 1;
    while (i <= N) {
        sum += i;
        ++i;
    }

    std::cout << "Sum from 1 to " << N << " = " << sum << '\n';
    return 0;
}

A faster alternative (constant time) is the arithmetic-series formula sum = N * (N + 1) / 2. That is fine for typical classroom sizes, but be careful: the multiplication can overflow for very large N. Use long long for larger ranges and consider 128-bit intermediates (__int128 on GCC/Clang) or explicit checks if you expect huge inputs.

Troubleshooting tips: test small values (N=1, 5, 100 -> 1, 15, 5050), check std::cin.fail() for non-numeric input, and compile with warnings (-Wall -Wextra) so the compiler helps catch simple mistakes.

Recommended Answers

All 5 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.