Results 1 to 4 of 4

Thread: simple loop

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166

    Angry 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

  2. #2
    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;
    }

  3. #3
    denniswrenn
    Guest
    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;
    }

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    thanks!

    it works now!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width