|
-
Aug 23rd, 2001, 11:38 AM
#1
Thread Starter
Addicted Member
simple loop
Hi!
I've tried to make a program that asks the user to enter 2 numbers and then take the first number and add the coming to that one until you've reached the top number that was entered by the user.
Simple; if the user enters 1 and 5, the program would calculate and add toghether all the numbers between and included the entered numbers (1 & 5).
any ideas?
thanks in advance!
/praetorian
-
Aug 23rd, 2001, 11:49 AM
#2
Member
Code:
#include <iostream.h>
int main()
{
int end;
cout << "End? ";
cin >> end;
if (end <= 0)
{
cout << "End must be greater than 0" << endl;
return 1;
}
int sum = 0; // important to initialize it!
for (int i = 1; i <= end; i++)
{
sum += i;
}
for (i = 1; i <= end)
{
cout << i;
if (i != end)
{
cout << " + ";
}
}
cout << " is " << sum;
return 0;
}
-
Aug 23rd, 2001, 11:56 AM
#3
Code:
int numaddthing()
{
int num1, num2, i, res = 0;
cout << "Enter first number: "; cin >> num1;
cout << "Enter second number: "; cin >> num2;
for(i = num1; i <= num2; i++)
{
res += i;
}
return res;
}
-
Aug 23rd, 2001, 11:58 AM
#4
Thread Starter
Addicted Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|