Rogelio Luna
Rogelio Luna

Reputation: 11

Count Multiples

I have been assigned to write a java program for class that counts multiples. The program takes three integers as input: low, high, x. The program then outputs the number of multiples of x between low and high exclusively.

If the input is: 1, 10, 2

the output would be: 5

My teacher has the proclivity to assign problems we haven't covered in class and I am unsure where to begin. Im not asking for the full code, just how to set up the problem

I am unsure of how to follow my logic thru the program: what I have so far is this

import java.util.Scanner;

public class LabProgram {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      
      int low, high, x;
      int count = 0;
      
      low = scnr.nextInt();
      high = scnr.nextInt();
      x = scnr.nextInt();
      
      for(int i = low; i <= high; i++){
         
         if(i % x == 0){
            count++;
         }
         
         if (i % x != 0){
            
            //System.out.println(count);// someone suggested to move this
         }
      }
      System.out.println(count);  
   }
}
~~~~~~

If I input 1, 10, 2

My output is 01234

Moved the print of count outside of the loop... man I am tired.

FINAL EDIT: This code works, it accomplishes the goal. Thank you to @charisma and everyone else that helped me understand what was going on here. I am new to java but determined to learn more! Thanks all!!!!!

Upvotes: 1

Views: 6065

Answers (3)

Nour Ibrahim
Nour Ibrahim

Reputation: 1

put count++; after the last print statement

Upvotes: -1

Piotr P. Karwasz
Piotr P. Karwasz

Reputation: 16055

Once you get to the basic implementation (as explained by Charisma), you'll notice, that it can take a lot of time if the numbers are huge: you have high - low + 1 iterations of the loop. Therefore you can start optimizing, to get a result in constant time:

  • the first multiple is qLow * x, where qLow is the ceiling of the rational quotient ((double) low) / x,
  • the last multiple is qHigh * x, where qHigh is the floor of the rational quotient ((double) high) / x,

Java provides a Math.floor() and Math.ceil(), but you can get the same result using integer division and playing with the signs:

final int qLow = -(-low / x);
final int qHigh = high / x;

Now you just have to count the number of integers between qLow and qHigh inclusive.

return qHigh - qLow + 1;

Attention: if x < 0, then you need to use qLow - qHigh, so it is safer to use:

return x > 0 ? qHigh - qLow + 1 : qLow - qHigh + 1;

The case x == 0 should be dealt with at the beginning.

Upvotes: 1

Charisma Kausar
Charisma Kausar

Reputation: 403

You can input numbers using scanner class similar to the following code from w3schools:

import java.util.Scanner;  // Import the Scanner class

class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter username");

    String userName = myObj.nextLine();  // Read user input
    System.out.println("Username is: " + userName);  // Output user input
  }
}

low, high and x can be of data type int.

To check which numbers between low and high are multiples of x, you can use a for loop. You can declare a new variable count that can be incremented using count++ every time the for loop finds a multiple. The % operator could be useful to find multiples.

You can output using System.out.println(" " + );

Edit:

% operator requires 2 operands and gives the remainder. So if i % x == 0, it means i is a multiple of x, and we do count++.

The value of i will run through low to high.

for (i = low; i <= high; i++) {
    if (i % x == 0) {
        count++;
    }
}

Upvotes: 1

Related Questions