I tried to search for other threads to answer my question but couldnt come up with anything that I could think of to describe what I was looking for well. My assignment is this:

Write an application that shows the sum of 1 to n for every n from 1 to 50. That is, the program prints 1 (the sum of 1 alone), 3 (the sum of 1 and 2), 6 (the sum of 1, 2, and 3), 10 (the sum of 1, 2, 3, and 4), and so on. This is what I have so far but I cant seem to figure it out. I keep getting different ways to get the total of adding the numbers 1 through 50 (1276).

// EverySum.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
	int n = 1;
	int count;
		

	for(count = 1; count <= 50; ++count)
		n = n + count;
		cout<<n<<endl;
	getche();
		
}

Recommended Answers

All 4 Replies

You need curly brackets around the two statements that follow the for loop so that both the increment and the display occur. Start count at 0.

I thinks you just need to add { } round the loop part.
You add up count each time but then only output n at the end.

If you output n for each step of the loop you will get exactly what you want.

Heres what it ended up looking like. Thanks for the help.

#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
	int n = 0;
	int count = 0;
	int sum;
		

	for(count = 1; count <= 50; ++count)
	{
		n = n + count;
		cout<<n<<endl;
	}
		getche();
		
}
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.