# How to find next number divisible by n

85 Ansichten (letzte 30 Tage)
Pedro Oliveira am 22 Nov. 2017
Kommentiert: Pedro Oliveira am 22 Nov. 2017
Hi, I have a number 'a' and I want to find the next number 'b' that is divisible by a certain n (if 'a' isn't divisible by n).
Initially I was doing it like below:
b = a + rem(a,n)
But I noticed that it gives the distance to the closest number that is divisible by n. In the specific example of being a = 14 and n = 4, rem(a,n) gives 2 since the closes number divisble by 4 is 12. Thus b will be 14 (not divisible by 4) instead of 16, as originally intended.
Thank you for any help you can provide.
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Torsten am 22 Nov. 2017
b = a + (n - rem(a,n))
Best wishes
Torsten.
##### 1 KommentarKeine anzeigenKeine ausblenden
Pedro Oliveira am 22 Nov. 2017
This works! Thank you!

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

David J. Mack am 22 Nov. 2017
b = n * ceil(a / n)
Greetings, David
##### 1 KommentarKeine anzeigenKeine ausblenden
Pedro Oliveira am 22 Nov. 2017
This also works! Thank you! I didn't know the ceil function but I'll look into it.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Mathematics in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by