Jump to content
Sign in to follow this  
whiteshirtguy

Coding Challenge: Multiples of a Number

Recommended Posts

The challenge is to take the input of two values AND/OR load two values from a text file in the following format: x,n where x and n are two integers. x represents some random number, whereas n represents a value that's a multiple of 2. n acts as an exponent. The file must have one list of numbers per line, so like so:

10,4
8,8
15,16

The goal is to have the output show the FIRST multiple of n that's greater than x. For the above input, we would see the following:

12
16
16

If you want to accept this challenge, the accepted languages are as follows:

Java (1.8) | Python 2 (2.7.3) | Python 3 (3.2.3)Bash (4.2.24) | C++ (4.8.1) | C# (4.0) | Lua (5.1.4) | JavaScript (1.8.5)

Due Date: December 13, 2015 @ 11:59pm UTC (EXPIRED)

Share this post


Link to post
Share on other sites

Solution:

Driver.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Driver {

	public static void main(String[] args) throws IOException {
		File file = new File("D:\\Java\\NumberMultiples\\src\\input.txt");
        BufferedReader in = new BufferedReader(new FileReader(file));
        String line;

        while ((line = in.readLine()) != null) {
            String[] lineArray = line.trim().split(",");

            int x = Integer.parseInt(lineArray[0]);
            int n = Integer.parseInt(lineArray[1]);

            System.out.println(findNextMultipleOfNGreaterThanX(x, n));
        }
	}
	
	public static int findNextMultipleOfNGreaterThanX(int x, int n) {
		int degree = 1;
		
		while(true) {
			int multiple = n * degree;
			
			if(multiple > x) {
				return multiple;
			}
			
			degree++;
		}
	}
}

input.txt

10,4
8,8
15,16

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...