Basic Loop

I'm working on this loop where i can add all odd integers. (ex: 1 + 3 + 5 + 7 = 16) I have to make sure any input value from 1-10000 will work. I don't know the next step and any hints or advice would be helpful. Thanks!


#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;
int main ()
{
int i = 1;
int sum = 0;
int howmany;

cout << "How many odd integers do you want to add? ";
cin >> howmany;

while (i <= 10000)
{
sum = sum + i;
i++;
}

cout << "The sum of the first " << howmany << " odd integers is " << sum << endl;

getch ();
return 0;
}
closed account (jwC5fSEw)
I'm a beginner, and I haven't had a chance to compile this and test it, but try this instead of the while loop you have now:

1
2
3
4
5
6
	if (howmany >= 1 && howmany <= 10000){
		for (n = howmany; n > 0; n--){
			sum += i;
			i += 2;
		}
	}


EDIT: Tested it, appears to work. Interestingly enough, the sum of n odd numbers equals n^2. I'm sure there's a good mathematical explanation for it, but I was surprised to see it.
Last edited on
Why not use shortcut formula for that?

Actually there is no need to use loops.

here is the formula for that

1
2
3
4
    sum = i*(i+1)/2 + (i-1)*i/2;
    //or simply
    sum = i*i;


sum = i*i;

what??? so simple like this! XD

EDIT: here is the complete code for that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main(){

	int howmany, sum;
	while(true){
		std::cout << "\nHow many odd integers do you want to add? ";
		std::cin >> howmany;
		if(howmany > 0 && howmany <= 1000)
			break;
		else
			std::cout << "\nPlease enter number from 1 to 1000 only"<<std::endl;
	}
	sum = howmany*howmany;
	std::cout << "The sum of the first " << howmany << " odd integers is " << sum << std::endl;
	return 0;
}
Last edited on
closed account (jwC5fSEw)
Oh wow. I realized that it was equal to the square of it, but it didn't occur to me to use it instead of loops. That's great.

One tiny change: for his program, it should be howmany instead of i.
the sum of n odd numbers equals n^2. I'm sure there's a good mathematical explanation for it, but I was surprised to see it


Actually I only derived that formula using this formula for the sum of consecutive numbers.

sum = n(n+1)/2; where n is a positive integer from [0,inf)
Last edited on
I tried all the ideas both of you have given me, but unfortunatley the answer that keeps coming up is: "The sum of the first 2 odd integers is 10000"

the sum of the first 2 odd integers (1 + 3) should equal 4.

any ideas?
closed account (jwC5fSEw)
Did you try the code posted above? Worked just fine for me.
I see you didn't revise your code

1
2
3
4
5
while (i <= 10000)
{
   sum = sum + i;//of course the loop exits at sum = 10000. 
   i++;
}


try changing it to this..

1
2
3
4
if(i >0 && i <=10000)
    sum = howmany*howmany;
else
    //do what you want here if the input is invalid 
It needs to be in a while loop, not an "if else" statement; it's required. Otherwise, I would immediatly use the code posted above!
while (i <= 10000)

what the! of course it will output sum == 10000.

using while loop? here I think is a bad idea. It makes the program less efficient.

But let see.. I can only implement while loop with this one.

1
2
3
4
5
6
7
8
    int cnt = 1, i = 1, sum = 0;
    while(cnt <= howmany)    //not <=10000 did you get it?
    {
        sum += i;
        i += 2;
        cnt++;
    }
     //this should work fine using your code 
Last edited on
Topic archived. No new replies allowed.