Here I have another one:
The program that should first read in two values from the keyboard, a starting value and an ending value. It should sum all the even numbers between those two values including the endpoints and display the sum. If the ending point is less than the starting point, the sum should be 0.
A sample dialog for this program looks as follows:

Enter starting and ending: 12 15
Sum of evens = 26
The loop that is required in this program is a counter-controlled loop, which is an example of definite iteration.

it is easy to see which are even and which are odd numbers but i do not know how i store all the even numbers. I know how to store one in a variable but they are more.
I just need just a hint to know what to think about.
thanks

Recommended Answers

All 7 Replies

It's not necessary to store all the even numbers. All you have to do is add the value of the loop counter to the sum accululator if the loop counter is even.

Hints :

1) Use a forloop to sum values?
2) Use explicit formula to calculate sum of even integers?
3) Use standard library instead of writing forloops?

I wrote this code but I don't know why the loop that I wrote doesn't want to increment the value if it is an odd number to an even number.

int main()
{
    int first,second,sum = 0,i,j;
    cout<< "Enter first and second :";
    cin>>first>>second;
    if(second<first)
     {
                    sum=0;
                    }
     else
          {
                    if(first %2==1,second%2==1)
                    {
                            i = first + 1;
                            j =second + 1;
                             sum=i+j;
                             }
                    if(first%2==0,second%2==0)
                     {
                       sum=first + second;
                       }
                       }
                     cout<< sum;
                    
                             
                             
           system("pause");
     return 0;
     }

you have to use a for loop

for(int i = first+1; i < last; i++)
{
   if( (i%2) == 0)
   {
      sum += i;
   }
}

I understood that I need a for loop to increment the first number but I do not need an expression like this for the second one also?
I do not understand the expression sum+ =i, it increments sum? and if it does why?

I understood that I need a for loop to increment the first number but I do not need an expression like this for the second one also?
I do not understand the expression sum+ =i, it increments sum? and if it does why?

sum+ =i ;

is equivalent to

sum=sum+i ;

>>It should sum all the even numbers between those two values

That's simple 5th grade (or less) math. If you have problems comprehending that requirement then you need to sharpen up your basic math skills.

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.