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.

Recommended Answers

All 4 Replies

1) 55

I would suggest start with a loop to iterate through the numbers and add them together.
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...

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.