lundi 30 avril 2018

C++ - Display only last result in fum of series in for loop

I'm writing a code to sum a series of numbers but it keep showing all the results. I only need the last result that is the sum of numbers

My Code:

//Sum of numbrs which are divisible by either 3 or 5
cout << "\nSum of numbers: ";

for ( numbers = 1; numbers <= limit; numbers++ )
{
    if ( ( numbers%3 == 0 ) || ( numbers%5 == 0 ) )
    {
        if (  !( numbers%3 == 0 && numbers%5 == 0 )  )
        {
            sum += numbers; 
            cout << sum << " ";
        }
    }
}

Output:

Sum of numbers: 3 8 14 23 33 45 63 83 104

I only need the last value as 104 in the above example.

Aucun commentaire:

Enregistrer un commentaire