Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if integer is multiple of 8

Tags:

c++

Hi i'm new to c++ so i'm not sure if this is a really silly question. Basically i'm using a c++ custom action project to interact with my MSI installer. I get a property that my user will have entered, it is an integer. I need to ensure that this is a multiple of 8 and i'm not sure how to go about this. Obviously if it can be divided by 8 it is a multiple but I am not sure how to capture if there is a remainder. Any help would be appreciated or even point me in the right direction. Thanks

like image 290
Natalie Carr Avatar asked Sep 20 '12 10:09

Natalie Carr


People also ask

How do you find the multiples of 8?

To find the multiples of 8 we must be familiar with the multiplication table of 8. Multiples of 8 will be the product of 8 with any natural number i.e 8n, where n is any natural number. Now, for the ten’s digit in each column in Row 1 will be written from 0 to 4 as:

How do you check if a number is a multiple?

whenever a number x is a multiple of some number y, then always x % y equal to 0, which can be used as a check.

How do you check if x is a multiple of Y?

whenever a number x is a multiple of some number y, then always x % y equal to 0, which can be used as a check. So use if (j % 4 == 0)

Is 8 a factor of 8 or multiple of 8?

However, 8 is a factor of 8, and also 8 is a multiple of 8. What are Multiples of 8? The multiples of 8 are the numbers that are generated when 8 is multiplied by any natural number.


4 Answers

Use the "modulo" operator, which gives the remainder from division:

if (n % 8 == 0) {
    // n is a multiple of 8
}
like image 196
Mike Seymour Avatar answered Sep 18 '22 13:09

Mike Seymour


Use the "modulo" or "integer remainder operator" %:

int a = ....;
if (a % 8 == 0 ) {
  // a is amultiple of 8
}
like image 28
juanchopanza Avatar answered Sep 18 '22 13:09

juanchopanza


use operator %

if ( num % 8 == 0 )
{
    // num is multple of 8
}
like image 34
Carlos Morales Avatar answered Sep 21 '22 13:09

Carlos Morales


Checking only the last 3 digits of a number does the job.
Even if you are given a huge number in the form of a string where the % operating is not useful you can check if only the last 3 digits are divisible by 8 then the whole number is divisible by 8.

like image 26
Rishabh Deep Singh Avatar answered Sep 19 '22 13:09

Rishabh Deep Singh