I need to use a for loop to print out the odd numbers between 25 - 1.

program2
1) I also need to use a for loop to prompt user to input two intergers: firstNumber and secondNumber (firstNumber must be less then secondNumber)
2) Outputs all odd numbers between firstNumber and secondNumber
3) output the sum of all even numbers between firstNumber and secondNumber
4) output the sum of the square of the off numbers between firstNumber and secondNumber (take each one, and then add them together)\

Please help me

Member Avatar for iamthwee

Show what you have so far.

Start at the top of the list you posted and do them just one at a time. Don't attempt to do them all at once. Do the first requirement then compile, correct bugs, and compile again until all problems have been fixed. Only then do you begin the next statement.

for (i=25; 25>=i; i=i-2)
{
//if (i%2!=0)
cout<<i<<" ";
}

cout<<endl;

if you observe carefully.. You will find the error.

this is what you have written:

for (i=25; 25>=i; i=i-2)

in the above observe this

25>=i;

it should actually show the lower parameter of your problem.

for (i=25; 25>=i; i=i-2)
{
//if (i%2!=0)
cout<<i<<" ";
}


cout<<endl;

how do I get this to not be an Infinite loop

for( i = 25; i >= 1; i--) Check that i is greater than 1. In your code 25 will always be greater than i because i starts out at 25 and goes down from there.

In the above loop you would have to check for every i if it is odd or not using

if(i%2!=0)

alternatively you could use i=i-2 instead of i-- and simply display the numbers.

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.