answersLogoWhite

0


Best Answer

First work out the algorithm for what you want to do and then encode that using the C++ language.

Alternative Answer

In this case, the algorithm will consist of a loop that counts from 1 to 1000, testing each value to see if it can be divided by 3 or 5 without leaving a remainder. If so, it gets added to a running total. When the loop ends, the running total is the final answer.

To determine a remainder in C++, we use the modulo operator (%). That is; 9%5 is 4 because 9 divided by 5 is 1 remainder 4, whereas 9%3 is zero. Therefore if either operation evaluates to zero, the value can be added to the running total.

int DoSum( void )

{

int total=0;

for(int x=1; x<1000; ++x)

if( !(x%3) !(x%5) )

total+=x;

return(total);

}

An alternative (and more efficient) algorithm will perform the same loop twice, first in steps of 3 and then in steps of 5. Since we're no longer testing for invalid values, we simply sum every value.

int DoSum( void )

{

int total=0;

for(int y=0; y<2; ++y)

{

int step=y?5:3; // if y is 0, step is 3, otherwise step is 5

for(int x=step; x<1000; x+=step)

total+=x;

}

return(total);

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a programme that prints the sum of all the multiples of 3 or 5 below 1000 in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

manthan?

Write the first 6 Multiples of 12 and 14.


How can you write a sentence with prints and prince?

The prince has a lot of prints that he printed (iI don't know I know it's dumb)


Write the four multiples of 65?

65,130,195,260


How do write searching number programme in java?

give description of that then i will send the answer


How do you write a letter for remove your finger prints?

Ask a local attorney.


Write 2 multiples of 2?

4 and 6


What are the common multiples of 14?

14284256708498112126140CyberchiefIf this answer helped you and you appreciate the fact that time was taken out of somebody's personal day to write the answer to this question, please support the contributor by clicking "Recommend Contributor" button below at the bottom of this answer box.


Write a c programme to draw a rainbow?

Write a C program to Draw a RAINBOW and fill the suitable colors ...


Multiples of 5 in even number write in rule form?

Even multiples of 5 must end with a zero.


Write down the first 3 multiples of 7?

The first 3 multiples of 7 are: 7, 14, and 21.


Identify the first three common multiples of 4 and 5?

write the first three common. multiples of 3,4,5


Write a C programme to count the number of vowals present in your name?

abhimanyu