## C++ || How To Get The Next & Previous Multiple Of A Number Using C++

The following is a module with functions which demonstrates how to get the next and previous multiple of a number using C++.

If a number is already a multiple, there is a parameter that allows you to specify if it should be rounded or not.

Click here for sample code demonstrating how to round a number by a specific amount.

1. Get Next Multiple – Include Existing

The example below demonstrates how to round up a number to the next multiple. In this example, if a number is already a multiple, it will not be rounded up to the next multiple.

``` 1. Get Next - Include Existing C++ // Get next multiple - include if already a multiple. int multiple = 5; for (int index = 1; index <= 10; ++index) { std::cout << index << " => " << Utils::getNextMultiple(index, multiple) << std::endl; } // expected output: /* 1 => 5 2 => 5 3 => 5 4 => 5 5 => 5 6 => 10 7 => 10 8 => 10 9 => 10 10 => 10 */ 12345678910111213141516171819202122 // Get next multiple - include if already a multiple.  int multiple = 5; for (int index = 1; index <= 10; ++index) {    std::cout << index << " => "        << Utils::getNextMultiple(index, multiple) << std::endl;} // expected output:/*    1 => 5     2 => 5     3 => 5     4 => 5     5 => 5     6 => 10     7 => 10     8 => 10     9 => 10     10 => 10*/ ```

2. Get Next Multiple – Skip Existing

The example below demonstrates how to round up a number to the next multiple. In this example, if a number is already a multiple, it will be rounded up to the next multiple.

``` 2. Get Next - Skip Existing C++ // Get next multiple - skip if already a multiple. int multiple = 5; // Set 3rd parameter to true to skip numbers that are already a // multiple to the next value for (int index = 1; index <= 10; ++index) { std::cout << index << " => " << Utils::getNextMultiple(index, multiple, true) << std::endl; } // expected output: /* 1 => 5 2 => 5 3 => 5 4 => 5 5 => 10 6 => 10 7 => 10 8 => 10 9 => 10 10 => 15 */ 123456789101112131415161718192021222324 // Get next multiple - skip if already a multiple.  int multiple = 5; // Set 3rd parameter to true to skip numbers that are already a // multiple to the next valuefor (int index = 1; index <= 10; ++index) {    std::cout << index << " => "        << Utils::getNextMultiple(index, multiple, true) << std::endl;} // expected output:/*    1 => 5     2 => 5     3 => 5      4 => 5     5 => 10      6 => 10      7 => 10      8 => 10      9 => 10      10 => 15*/ ```

3. Get Previous Multiple – Include Existing

The example below demonstrates how to round down a number to the previous multiple. In this example, if a number is already a multiple, it will not be rounded down to the previous multiple.

``` 3. Get Previous - Include Existing C++ // Get previous multiple - include if already a multiple. int multiple = 5; for (int index = 10; index >= 1; --index) { std::cout << index << " => " << Utils::getPreviousMultiple(index, multiple) << std::endl; } // expected output: /* 10 => 10 9 => 5 8 => 5 7 => 5 6 => 5 5 => 5 4 => 0 3 => 0 2 => 0 1 => 0 */ 12345678910111213141516171819202122 // Get previous multiple - include if already a multiple.  int multiple = 5; for (int index = 10; index >= 1; --index) {    std::cout << index << " => "         << Utils::getPreviousMultiple(index, multiple) << std::endl;} // expected output:/*    10 => 10     9 => 5     8 => 5     7 => 5     6 => 5     5 => 5     4 => 0     3 => 0     2 => 0     1 => 0*/ ```

4. Get Previous Multiple – Skip Existing

The example below demonstrates how to round down a number to the previous multiple. In this example, if a number is already a multiple, it will be rounded down to the previous multiple.

``` 4. Get Previous - Skip Existing C++ // Get previous multiple - skip if already a multiple. int multiple = 5; // Set 3rd parameter to true to skip numbers that are already a // multiple to the previous value for (int index = 10; index >= 1; --index) { std::cout << index << " => " << Utils::getPreviousMultiple(index, multiple, true) << std::endl; } // expected output: /* 10 => 5 9 => 5 8 => 5 7 => 5 6 => 5 5 => 0 4 => 0 3 => 0 2 => 0 1 => 0 */ 123456789101112131415161718192021222324 // Get previous multiple - skip if already a multiple.  int multiple = 5; // Set 3rd parameter to true to skip numbers that are already a // multiple to the previous valuefor (int index = 10; index >= 1; --index) {    std::cout << index << " => "        << Utils::getPreviousMultiple(index, multiple, true) << std::endl;} // expected output:/*    10 => 5     9 => 5     8 => 5      7 => 5     6 => 5     5 => 0     4 => 0     3 => 0     2 => 0     1 => 0*/ ```

5. Utils Namespace

The following is the Utils.js Namespace. Include this in your project to start using!

``` Utils Namespace C++ // ============================================================================ // Author: Kenneth Perkins // Date: Mar 3, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.h // Description: Handles general utility functions // ============================================================================ #pragma once #include <cmath> namespace Utils { /** * FUNCTION: getNextMultiple * USE: Returns the next multiple of a given number. Unless specified, * it will not round up numbers which are already multiples * @param number: The given number to find the next multiple * @param multiple: The multiple number * @param skipAlreadyMultiple: Boolean that specifies if input numbers which * are already multiples should be skipped to the next multiple or not * @return: The next multiple of the given number */ int getNextMultiple(int number, int multiple, bool skipAlreadyMultiple = false) { int result = 0; if (multiple != 0) { int remainder = (std::abs(number) % multiple); if (!skipAlreadyMultiple && remainder == 0) { result = number; } else if (number < 0 && remainder != 0) { result = -(std::abs(number) - remainder); } else { result = number + (multiple - remainder); } } return result; } /** * FUNCTION: getPreviousMultiple * USE: Returns the previous multiple of a given number. Unless specified, * it will not round down numbers which are already multiples * @param number: The given number to find the previous multiple * @param multiple: The multiple number * @param skipAlreadyMultiple: Boolean that specifies if input numbers which * are already multiples should be skipped to the previous multiple or not * @return: The previous multiple of the given number */ int getPreviousMultiple(int number, int multiple, bool skipAlreadyMultiple = false) { return getNextMultiple(number, multiple, !skipAlreadyMultiple) - multiple; } }// http://programmingnotes.org/ 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 // ============================================================================//    Author: Kenneth Perkins//    Date:   Mar 3, 2021//    Taken From: http://programmingnotes.org///    File:  Utils.h//    Description: Handles general utility functions// ============================================================================#pragma once#include <cmath> namespace Utils {    /**    * FUNCTION: getNextMultiple     * USE: Returns the next multiple of a given number. Unless specified,    *   it will not round up numbers which are already multiples    * @param number: The given number to find the next multiple    * @param multiple: The multiple number    * @param skipAlreadyMultiple: Boolean that specifies if input numbers which    *   are already multiples should be skipped to the next multiple or not    * @return: The next multiple of the given number    */    int getNextMultiple(int number, int multiple, bool skipAlreadyMultiple = false) {        int result = 0;        if (multiple != 0) {            int remainder = (std::abs(number) % multiple);            if (!skipAlreadyMultiple && remainder == 0) {                result = number;            } else if (number < 0 && remainder != 0) {                result = -(std::abs(number) - remainder);            } else {                result = number + (multiple - remainder);            }        }        return result;    }     /**    * FUNCTION: getPreviousMultiple     * USE: Returns the previous multiple of a given number. Unless specified,    *   it will not round down numbers which are already multiples    * @param number: The given number to find the previous multiple    * @param multiple: The multiple number    * @param skipAlreadyMultiple: Boolean that specifies if input numbers which    *   are already multiples should be skipped to the previous multiple or not    * @return: The previous multiple of the given number    */    int getPreviousMultiple(int number, int multiple, bool skipAlreadyMultiple = false) {        return getNextMultiple(number, multiple, !skipAlreadyMultiple) - multiple;    }}// http://programmingnotes.org/ ```

6. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

``` More Examples C++ // ============================================================================ // Author: Kenneth Perkins // Date: Mar 3, 2021 // Taken From: http://programmingnotes.org/ // File: program.cpp // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ #include <iostream> #include <string> #include <exception> #include "Utils.h" void display(const std::string& message); int main() { try { int multiple = 5; for (int index = 1; index <= 10; ++index) { std::cout << index << " => " << Utils::getNextMultiple(index, multiple) << std::endl; } display(""); // Set 3rd parameter to true to skip numbers that are already a // multiple to the next value for (int index = 1; index <= 10; ++index) { std::cout << index << " => " << Utils::getNextMultiple(index, multiple, true) << std::endl; } display(""); for (int index = 10; index >= 1; --index) { std::cout << index << " => " << Utils::getPreviousMultiple(index, multiple) << std::endl; } display(""); // Set 3rd parameter to true to skip numbers that are already a // multiple to the previous value for (int index = 10; index >= 1; --index) { std::cout << index << " => " << Utils::getPreviousMultiple(index, multiple, true) << std::endl; } } catch (std::exception& e) { display("\nAn error occurred: " + std::string(e.what())); } std::cin.get(); return 0; } void display(const std::string& message) { std::cout << message << std::endl; }// http://programmingnotes.org/ 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 // ============================================================================//    Author: Kenneth Perkins//    Date:   Mar 3, 2021//    Taken From: http://programmingnotes.org///    File:  program.cpp//    Description: The following demonstrates the use of the Utils Namespace// ============================================================================#include <iostream>#include <string>#include <exception>#include "Utils.h" void display(const std::string& message); int main() {    try {        int multiple = 5;         for (int index = 1; index <= 10; ++index) {            std::cout << index << " => "                << Utils::getNextMultiple(index, multiple) << std::endl;        }         display("");         // Set 3rd parameter to true to skip numbers that are already a         // multiple to the next value        for (int index = 1; index <= 10; ++index) {            std::cout << index << " => "                << Utils::getNextMultiple(index, multiple, true) << std::endl;        }         display("");         for (int index = 10; index >= 1; --index) {            std::cout << index << " => "                 << Utils::getPreviousMultiple(index, multiple) << std::endl;        }         display("");         // Set 3rd parameter to true to skip numbers that are already a         // multiple to the previous value        for (int index = 10; index >= 1; --index) {            std::cout << index << " => "                << Utils::getPreviousMultiple(index, multiple, true) << std::endl;        }    } catch (std::exception& e) {        display("\nAn error occurred: " + std::string(e.what()));    }     std::cin.get();    return 0;} void display(const std::string& message) {    std::cout << message << std::endl;}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.