Thread: Nearest divisible number of n by k

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    1

    Post Nearest divisible number of n by k

    I am new to programming so I have a problem, I have a task that says enter n and k without using any loops find the number that is closest to and divisible by k , number can only be bigger then n.
    Only thing that I can think of is

    Code:
    if (n%k==0){
        found_number=n;
    }else {
        while(1){
        n++;
        if (n%k==0){
            found_number=n;
            break;
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If 'n' is not evenly divisible by 'k', there will be a remainder. Perhaps this remainder will be useful in determining the result.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number divisible by 6
    By artistunknown in forum C# Programming
    Replies: 2
    Last Post: 03-25-2014, 10:14 PM
  2. Replies: 4
    Last Post: 02-11-2011, 03:29 PM
  3. problem with program number divisible by 10? using macros
    By MarjunC in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2010, 05:43 AM
  4. Displaying Numbers divisible by user's desired number
    By DaniiChris in forum C Programming
    Replies: 9
    Last Post: 07-07-2008, 02:06 PM
  5. Calculate nearest number?
    By electrolove in forum C Programming
    Replies: 10
    Last Post: 02-21-2003, 06:13 AM

Tags for this Thread