question related to C++
1) Find the sum of natural numbers from 1 to 10.
2) Accept n numbers and find total of divisible by 5 and not by 10.

4
Contributors
4
Replies
21
Views
2 Years
Discussion Span
Last Post by rubberman

1) 55

Something like:

``````int sum=0;          //variable to accumulate the numbers
int natNum=1;

while(natNum<=10)    //while loop continually iterates to your limit.
{
sum=sum+natNum; //accumulates number into the sum
++natNum;       //increments number by 1
}
``````

That's the only clue I'm giving right now...see if you can figure out the division part.

Sorry, I didn't take enough time putting that out there. It should be more like:

``````    int sum=0;          //variable to accumulate the numbers
int natNum=0;
while(natNum<=10)    //while loop continually iterates to your limit.
{
sum=sum+natNum; //accumulates number into the sum
++natNum;       //increments number by 1
cout<<sum<<" "<<endl;  //output for testing purposes.
}
``````

2) for numbers > 9, if it ends in 0, it is divisible by 10. If it ends in a 5, it is divisible by 5. If < 9, it isn't divisible by 10, but if it == 5, then it is divisible by 5. Done...

Edited by rubberman

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.