The question is "write a program that request the user to enter two integers. The program should then calculate and report the sum of all the integers between and including the two integers. At this point, assume that the smaller integer is entered first. For example, if the user enters 2 and 9, the program should report the sum of all the integers from 2 through 9 is 44."

I have to use 2 integers, a for loop etc.. I just dont know how to make this program as smalles as posibile without making an array for instance, I hope someone can help me with this if possible.

Recommended Answers

All 13 Replies

Show us what you've done so far.

Lol. Im a beginner though, I deleted the old code since I got infinite loops.. This is the code I have now, im not sure to make nested for loop or if im even in the right track.. Please bear with me :)

#include <iostream>

using namespace std;

int main(){

int first = 0;
int second = 0;
int value = first + second;

cout << "Enter first Integer: ";
cin >> first;
cout << "Enter second Integer: ";
cin >> second;

for (int first = 0; first < second; first + second, first++){
	
	
	cout << "value of the entered numbers" <<" :"<< first <<endl;
	
}



cin.get();
cin.get();
return 0;
}

Im not sure to use an array yet to look for the next number and plus it. I cant express it in code, its probally simple but I cant see it. Yet

Here's a hint: You don't need an array.

Does your compiler not have a problem with line 16(i.e declaring a variable twice)?

No the code runs... I use visual studio 2010.. Is that a problem in other ides?

Thread solved. I figured it out with a little help. Thx guys for taking the time

you need to mark it solved

Does your compiler not have a problem with line 16(i.e declaring a variable twice)?

No, there's nothing wrong with it. The two definitions are in different scopes.

can you elaborate?
sorry, would you elaborate?

for (int i = 0;i<10;i++)
{
    std::cout << i;  //works
}

std::cout << i; //who?? doesn't work, i is destroyed once the loop ends.
int i = 0;

for(;i<10;i++)
{
   std::cout << i; //works
}

std::cout<< i;  //also works

Anytime you open up braces it's a new scope. I believe, but am not 100% sure, that the standard considers the statements in the parentheses of the for loop to be part of the loop body.

You can calculate the result using AP (Arithmetic Progression formula).

Take small number say x.
SumX = (x*(x+1)) / 2;

Now take bigger number say y.

SumY = (y*(y+1)) / 2;

Now Subtract
Result = SumY - SumX;

No need of any array.

-Dhiraj

Result = SumY - SumX;

Would it not be
Result=SumY-SumX+x;?

#include <iostream>

using namespace std;

int main(){

int first = 0;
int second = 0;
int value = 0;

cout << "Enter first Integer: ";
cin >> first;
cout << "Enter second Integer: ";
cin >> second;

for (; first <= second; first++)
{
value += first;
}
cout << "Sum of the entered numbers" <<" :"<< value <<endl;


cin.get();
cin.get();
return 0;
}

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.