Hey, guys. I'm new to this forum and this is my first post ever on this site. I am currently a freshman in Penn State University and I am having trouble with my C++ class. I have a group project but have no idea where to start with this program I have to make.

I am not asking for the code, I'm just asking for help so that I can actually start writing it and hopefully executing it correctly.

Here are the details:
P1. [Divisibility by 3 and 9]
Let n = akak-1ak-2 … a1a0 be a positive integer. Let s = ak + ak-1 + ak-2 + …+ a1 + a0
be a the sum of the digits of n. It has been proved that n is divisible by 3 and 9 if s is
divisible by 3 and 9.
Examples:
425175 is divisible by 3 because 4 + 2 + 5 + 1 + 7 + 5 = 24 and 24 is divisible by 3.
23454 is divisible by 9 because 2 + 3 + 4 + 5 + 4 = 18 and 18 is divisible by 9.
Write a C++ program that prompts the user to enter a positive integer and then uses this
criterion to determine whether the number is divisible by 3 and/or 9
Hints:
(1) A positive integer n is divisible by positive integer k if n%k == 0.
(2) 425175%10
5 and 425175/10
42517

the squares are arrows going to the right

When it gets to the akak, the second thing after the a's are all subscripts. I just need to know where to get started. Thanks everyone (:

## Recommended Answers

I would start with

``````int main()
{
code
more code

}``````

LOL... Can you read the input in as a string and separate the digits for the calculation?

Go incrementally.

Start by learning Basic Input/Output in C++.

Then, try to print all the digits of an input number (as an `int` ). To extract the digits, you are going to need a

Ok. Now that you are almost there, I'll complete it.

Basically, the first two lines in your loop are almost correct. The other two lines should be taken out. This would give you this loop:

``````//Statement
while(n > 0)
{
sum += n % 10; //notice …``````

## All 10 Replies

I would start with

``````int main()
{
code
more code

}``````

LOL... Can you read the input in as a string and separate the digits for the calculation?

I believe so. All it has to do is be able to run. I know that it has to be the #include <iostream>
using namespace std;
int main()

i know all that. I just don't know what to put as my values like my s or k or n or anything like that. this project is screwing me over

Go incrementally.

Start by learning Basic Input/Output in C++.

Then, try to print all the digits of an input number (as an `int` ). To extract the digits, you are going to need a while-loop and the second hint given to you (about 425175 % 10 = 5 and 425175 / 10 = 42517).

Finally, once you can print the digits, you can easily sum them up and use your first hint to test if that sum is divisible by 3 and/or 9.

Okay, I got this far. Where does the integer k come in?

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

int main()
{
int n;
cout << "Write down random numbers from 0-9 and I will add them             together." << endl;
cin >> n;
while(n > 0)
{
``````

>>Where does the integer k come in?

Forget about the integer k, it is not relevant. Just go about steps I described in my previous post. Don't forget to compile and run your code very often, and play with it. You're doing good so far, keep going, step by step.

okay, I wrote the code, but realized that it is an endless loop and did not find a way to add them yet. but here is what I have:

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

int main()
{
int n;
cout << "Write down random numbers from 0-9 and I will add them together." << endl;
cin >> n;
while(n > 0)
{
if(n%9 == 0)
cout << "The number is divisible by both 3 and 9." << endl;
else if(n%3 == 0)
cout << "The number is divisible by 3." << endl;
else
cout << "The number is not divisible by 3 or 9" << endl;
}
return 0;
}``````

I know that adding them has to be in the loop somewhere, but I don't know how. Do I have to add another integer or must I do something like n--?

Your are jumping the guns here. I laid out three steps to do, you jumped over the second step, which is screwing up your last step.

So, to recap, what you need to do is this:

1) Ask the user for a number, and store that number in an integer variable.

--> This is done in your lines 6, 7 and 8.

2) Iterate through the digits of the number. In the iteration you should first print out each digit to make sure your loop is working correctly. Then, you should sum up the digits.

--> You skipped this step. This is the only step that must be done in a loop.

3) Check if the sum of digits is divisible by 3 and/or 9.

--> You did this inside a loop, which doesn't make sense, this test only needs to occur once, once you have the sum of digits.

So, starting from your code, you can make the skeleton of your program by simply separating the three steps clearly:

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

int main()
{
// 1) Prompt the user for a number:
int n;
cout << "Write down random numbers from 0-9 and I will add them together." << endl;
cin >> n;

// 2) Iterate through the digits to sum them up (print also for debugging):
int sum = 0; //the sum starts at 0.
while(n > 0)
{
//Here is the only piece of the puzzle that you are missing.
//In this iteration, you need to:
// optional) print the lowest digit of 'n' to 'cout'.
// a) add the lowest digit of 'n' to the 'sum' variable.
// b) eliminate the lowest digit from 'n'.
}

// 3) Test if the sum of digits is divisible by 3 and/or 9:
if(sum % 9 == 0)
cout << "The number is divisible by both 3 and 9." << endl;
else if(sum % 3 == 0)
cout << "The number is divisible by 3." << endl;
else
cout << "The number is not divisible by 3 or 9" << endl;

return 0;
}``````

Now, the hints that were given to you give you a means to perform the a) and b) steps within the loop. To make it even clearer, `n % 10` gives you the lowest digit in 'n', while `n / 10` gives you a value that eliminates the last digit of n.

At this point, I couldn't possibly take you any closer to the solution without doing your homework for you, which I won't do.

This is what I have and was able to do. It only works for the first 2 digits though and I know why, but I don't know how to fix it. Is there a way to chage my 10's to a higher number in the loop as the n value goes up?

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

int main()
{

//Variables
int n;
int sum = 0;

//input
cout << "Write down random numbers from 0-9 and I will add them together." << endl; //prompts user for a number
cin >> n;

//Statement
while(n > 0)
{
sum = n%10;
n = n/10;
sum = n + sum;
n = 0;
}

//output
cout << sum << endl;
if(sum % 9 == 0)
cout << "The number is divisible by both 3 and 9" << endl;
else if(sum % 3 == 0)
cout << "The number is divisible by 3" << endl;
else
cout << "The number is not divisible by 3 or 9" << endl;
return 0;
}
``````

Ok. Now that you are almost there, I'll complete it.

Basically, the first two lines in your loop are almost correct. The other two lines should be taken out. This would give you this loop:

``````//Statement
while(n > 0)
{
sum += n % 10; //notice '+=' here, which adds 'n % 10' to 'sum'.
n = n / 10;
}``````

I really don't know what to say. I never would have thought of that the = sign would solve it. But I guess that is why I am the student. Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.