how many numbers upto 1000 are there such that the sum of digits is divisible by 7 and the number itself is divisible by 3

How to write code for how many numbers upto 1000 are there such that the sum of digits is divisible by 7 and the number itself is divisible by 3

What have you tried so far? Show the code.

Do you know how to sum the digits of a number?

Do you know how to test if a number is divisible by another number?

Pseudocode:

count = 0
for n = 3 to 1000 by 3's:   // only test numbers that are divisible by 3
    if sum_digits(n) is divisible by 7
        ++count
print count

Last edited on
@tpb This is what I tried.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{

	for (int i = 0; i < 1000; i++)
	{
		if (i / 7)
		{
			cout << i;
		}
	}

	return 0;
}
First you should write a function to get the sum of digits and test it separately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int sum_of_digits(int num)
{
  // your code here
}

int main()
{
  int input;
  for(;;)
  {
    cout << "Enter a number(negative number to quit): ";
    cin >> input;
    if (input < 0)
      break;

      cout << "The sum of the digits is: " << sum_of_digits(input);
  }
}

To check if a number is divisible by another number you need to use to modulo operator.
https://www.cprogramming.com/tutorial/modulus.html
You don't need a function to calculate the sum of digits. Either the number is 0 or the sum of the digits is exactly 21. Two nested loops would do: the third digit is fixed by the requirement that they sum to 21.
@lastchance: You could have added rationale for the "exactly 21".

"sum of digits is divisible by 7" means that the sum is 7, 14, 21, 28, 35, ...
(I think that 0 should be omitted in this case.)
Of "numbers upto 1000" the 999 yields the largest sum of digits: 27.

"number itself is divisible by 3" has interesting property: its sum of digits is divisible by 3.
Therefore, only the 21 can be a sum digits of a number that is divisible by 3.


There are two approaches:
1. Brute force; test every number "upto 1000"
2. Use math; you need to test only the numbers that are divisible by 3 (like tpb suggested) and you can start from the smallest number that has digit sum of 21 (do you know what that is?)

Either way, you need to know how to test "is divisible", you have to count the numbers that match and you have to know how to use loops. Using a function is a bonus.

You did try:
1
2
3
4
5
6
7
for (int i = 0; i < 1000; i++)
{
  if (i / 7)
    {
      cout << i;
    }
}

There is a loop. Good.
It checks 1000 different numbers. Close. You check 0, but you don't check 1000.
"upto 1000" includes the 1000, does it not?

You test only one thing i / 7. What do we know about integer division?
IF i < 7
THEN i / 7 == 0 and 0 is false
ELSE i / 7 > 0 and that is true

In other words, your program does same as:
1
2
3
4
for (int i = 7; i < 1000; i++)
{
  cout << i;
}


That is in no way "test if x is divisible by y".

Furthermore, you had no attempt to "count the matches".
Yes, that rationale for 21 is sound. I should probably have explained.

But on that basis, no testing is necessary and you can use deterministic loops for a number ijk.

Smallest possible is 399.

Loop i from 3 to 9
Loop j from 21-i-9 to 9
k is then 21-i-j to get the sum right.
The number is recovered as 100i + 10j + k

If you wanted to include 0 as a trivial case put it at the start.

If you just want the number of such possibilities it could just be worked out as 1+2+...+7, the numbers corresponding to the number of possibilities for j.
Last edited on
Here is list of all the numbers based on the pseudo code of tpb.

399 ( 21 )
489 ( 21 )
498 ( 21 )
579 ( 21 )
588 ( 21 )
597 ( 21 )
669 ( 21 )
678 ( 21 )
687 ( 21 )
696 ( 21 )
759 ( 21 )
768 ( 21 )
777 ( 21 )
786 ( 21 )
795 ( 21 )
849 ( 21 )
858 ( 21 )
867 ( 21 )
876 ( 21 )
885 ( 21 )
894 ( 21 )
939 ( 21 )
948 ( 21 )
957 ( 21 )
966 ( 21 )
975 ( 21 )
984 ( 21 )
993 ( 21 )
Num: 28

Since we are dealing with a total newbie this seems to be the easiest way.
Topic archived. No new replies allowed.