Click here to Skip to main content
15,911,139 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: consecutive sums Pin
minikg19-Sep-01 12:28
minikg19-Sep-01 12:28 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 12:47
protectorChristian Graus19-Sep-01 12:47 
GeneralRe: consecutive sums Pin
minikg19-Sep-01 13:22
minikg19-Sep-01 13:22 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 14:23
protectorChristian Graus19-Sep-01 14:23 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 14:40
protectorChristian Graus19-Sep-01 14:40 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 14:42
protectorChristian Graus19-Sep-01 14:42 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 14:58
protectorChristian Graus19-Sep-01 14:58 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 16:32
protectorChristian Graus19-Sep-01 16:32 
Here we go - this works every time and uses while loops to get to where it's going.

#include "stdafx.h"

#include <iostream>
#include <math.h>

using std::cout;
using std::cin;
using std::endl;

int main(int argc, char* argv[])
{
	
	// It's a good idea to declare variables as you need them, not all at the top like this
	int input;

	cout <<"please enter a number ";
	cin >> input;

	int result = 1;

	int low = 1, high = 1;

// Figure out a sequence starting at 1 that is >= input

	while (result < input) 
	{ 
		result += ++high;
	}

// Until the sequence is equal to input we subtract from the bottom until it's less than input,
// then add at the top until its greater than or equal
// We use while loops because they will run however many times they need to in order to fulfill the condition
// We could equally have put for (;result != input;) but that would have been pointless.

	while (result != input)
	{
		while (result > input)
			result -=low++;

		while (result < input)
			result += ++high;
	}

// output the result

	for (int i = low; i < high; ++ i)
	{
		cout << i << " + ";
	}

	cout << high << " = " << result << endl;


	return 0;
}



Christian

As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.

Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
QuestionHow do you remove help from a project? Pin
Cathy18-Sep-01 14:21
Cathy18-Sep-01 14:21 
AnswerRe: How do you remove help from a project? Pin
Tomasz Sowinski19-Sep-01 2:02
Tomasz Sowinski19-Sep-01 2:02 
GeneralRe: How do you remove help from a project? Pin
Cathy19-Sep-01 10:52
Cathy19-Sep-01 10:52 
Questionhelp, Implement Flashwindow like MNS Msg? Pin
OGIR18-Sep-01 12:30
OGIR18-Sep-01 12:30 
AnswerRe: help, Implement Flashwindow like MNS Msg? Pin
Michael P Butler18-Sep-01 22:21
Michael P Butler18-Sep-01 22:21 
Generalchar* as template parameter Pin
Aaron Schaefer18-Sep-01 12:22
Aaron Schaefer18-Sep-01 12:22 
GeneralRe: char* as template parameter Pin
Michael Dunn18-Sep-01 21:16
sitebuilderMichael Dunn18-Sep-01 21:16 
GeneralRe: char* as template parameter Pin
Aaron Schaefer19-Sep-01 3:48
Aaron Schaefer19-Sep-01 3:48 
GeneralDebug vs Release Pin
18-Sep-01 11:58
suss18-Sep-01 11:58 
GeneralRe: Debug vs Release Pin
Tomasz Sowinski19-Sep-01 1:06
Tomasz Sowinski19-Sep-01 1:06 
GeneralWin32 Printing Problems - 32character device names. Pin
Mike Doner18-Sep-01 11:19
Mike Doner18-Sep-01 11:19 
GeneralWin2000/XP Job Kernel Object Pin
18-Sep-01 11:12
suss18-Sep-01 11:12 
GeneralMFC Extension DLL Pin
sfanjoy18-Sep-01 10:56
sfanjoy18-Sep-01 10:56 
GeneralRe: MFC Extension DLL Pin
Tomasz Sowinski18-Sep-01 11:10
Tomasz Sowinski18-Sep-01 11:10 
GeneralExecuting a database macro from VC++ Pin
duggie18-Sep-01 10:55
duggie18-Sep-01 10:55 
GeneralRe: Executing a database macro from VC++ Pin
Carlos Antollini18-Sep-01 11:01
Carlos Antollini18-Sep-01 11:01 
GeneralRe: Executing a database macro from VC++ Pin
duggie18-Sep-01 11:11
duggie18-Sep-01 11:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.