Need help with factor problem

I'm making a program that prompts a user for an int value. The value input by the user must be a multiple of 16. If it is not, I must return an error message. How would I go about doing this? I'm new to C++.

Any help is appreciated! Thanks!
Jack
What have you written so far? Be sure to put the code [code]between code tags[/code] so it formats properly.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

  int x;

  cout << "Enter a number that is a multiple of 16." << endl;
  cin >> x >> endl;

  if 
      (code for if x is a factor of 16)

   cout << "Great! You can follow directions. " << endl;

  else 
     
    cout << x << "is not a multiple of 16." << endl;

      return 1;
This is almost a complete program. You need to put this code into a main function and also add the includes and such.

On line 5, you cannot cin into endl ;)

On line 8, look up the modulus operator - I am sure you will quickly figure out how to use it.

On line 14, you may want a space at the beginning of the string so there is a space between the number and the text. I guess this space somehow ended up at the end of line 10.
You could try this:
1
2
3
4
5
6
//Modulus is dividing while returning the remainder.
//Example: 45 mod 16 returns 12 <- remainder;
//Then: 256 mod 16 returns 0 <- remainder;
bool multiple( int x , int y ) {
    return ( x % y ) == 0;
}

"%" is the modulus operator. Basically this function returns a boolean value(true(1) or false(0)) if x % y == 0 the function returns true else it returns false. How it works is that if value(x) is a multiple of value(y) then x % y does not return a remainder, it returns 0.

In your code you would call it as a function:
1
2
if ( multiple( 256 , 16 ) == true ) {
//etc. 
Last edited on
I would like to add onto FatalSleep's post and explain what the computer is doing when you use the modulus operator.

Let's use the example of 2 % 7.
First, the computer calculates 7/2.
Then, the computer sees if there are any units (ones, numbers. e.g: the number 2 has 2 units) left over.
If there are, the computer returns the number of units left over, mathematically called a remainder, if not, the computer returns 0.
Topic archived. No new replies allowed.