Hello all, I just started using c++ and I'm supposed to write a function that takes two positive integers, p and q, where p<q, and i have to return the sum of all the numbers >=p and <=q which are divisible by 7.

here's what i have so far:

``````# include <iostream>
using namespace std;

int d7 (int p, int q, int &s);

int main ()
{
int p=0;
int q=0;
int s=0;

cout << "Enter two positive integers" <<endl;
cin >> p;
cin >> q;

d7 (p,q,s);

cout <<s<<endl;

system ("pause");
return 0;
}
int d7 (int p, int q, int &s)
{
while (p<q) {
if(p%7==0, q%7==0){
s+=(p+q);}
q--;
}
}
``````

But it doesn't work!!! Help please...

It's much easier to read if you use code tags:

[code=cplusplus] //paste code here

[/code]

``if(p%7==0, q%7==0)``

I assume that comma is a mistake and you want &&, but what are you trying to do in this line?

thanks for the tip...I guess what i'm trying to say there is to take all the numbers between p and q that are divisible by 7...

thanks for the tip...I guess what i'm trying to say there is to take all the numbers between p and q that are divisible by 7...

``if(p%7==0, q%7==0)``

p doesn't change. q does, but p doesn't. Assuming you meant this:

``if(p%7==0 && q%7==0)``

What if p is 13? Will the if-statement ever be true? No. Therefore, should you be testing whether p is divisible by 7 in the if statement? No.

Consider redesigning the function so that neither p nor q changes. Create a third variable to loop through the values you want to check and test whether that variable is divisible by 7 (there are faster ways to get what you want, but this way will work and is along the lines of what you are thinking).

Consider redesigning the function so that neither p nor q changes. Create a third variable to loop through the values you want to check and test whether that variable is divisible by 7 (there are faster ways to get what you want, but this way will work and is along the lines of what you are thinking).

But would that give me the sum of all numbers >= p and <= q
which are divisible by 7. for example if p=4 and q=25 the sum should be 7+14+21=42...because i don't really care if the sum is divisible by 7. Maybe i'm not understanding correctly what you're trying to explain.
thanks for your time, i appreciate it...

But would that give me the sum of all numbers >= p and <=q
which are divisible by 7. for example if p=4 and q=25 the sum should be 7+14+21=42...because i don't really care if the sum is divisible by 7. Maybe i'm not understanding correctly what you're trying to explain.
thanks for your time, i appreciate it...

The sum WILL be divisible by 7 since all of the addends that make up the sum (7, 14, 21) are divisible by 7.

I am suggesting that you could set up a loop, as you have (I used a for-loop instead of a while loop, but you could use a while loop if you chose), but use neither p nor q as the loop control variable:

``````for (int i = p; i <= q; i++)
{
if (i % 7 == 0)
{
// i is a number between p and q that is divisible by 7.
// Do something.
}
}``````

I got it thank you!

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.