Question

We may expand the general strategy established in the prior question to determine whether a given...

We may expand the general strategy established in the prior question to determine whether a given number is divisible by another. Modify the program from the prior question or write one from scratch which determines whether 12345 is divisible by 15.

public class Main {

  public static void main( String[] args ) {

    int myNumber = 12345; //15

    int divisor = 15; //4

    System.out.print( "(" + myNumber + ") is " );

    if( myNumber%divisor==0) {

      // Do nothing

    }

    else {

      System.out.print( "not " );

    }

    System.out.println( "divisible by " + divisor + "." );

  }

}

Homework Answers

Answer #1
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int myNumber = in.nextInt(); //15
        System.out.print("Enter divisor: ");
        int divisor = in.nextInt(); //4
        System.out.print("(" + myNumber + ") is ");
        if (myNumber % divisor == 0) {
            // Do nothing
        } else {
            System.out.print("not ");
        }
        System.out.println("divisible by " + divisor + ".");
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
Modify the CountByFives application so that the user enters the value to count by. Start each...
Modify the CountByFives application so that the user enters the value to count by. Start each new line after 10 values have been displayed. (Cengage Mindtap assignment). This is the java code we are given: public class CountByAnything { // Modify the code below public static void main (String args[]) { final int START = 5; final int STOP = 500; final int NUMBER_PER_LINE = 50; for(int i = START; i <= STOP; i += START) { System.out.print(i + "...
Give the output of the following program. Then explain what the foo() method does, in general,...
Give the output of the following program. Then explain what the foo() method does, in general, for a given input. Is foo() a tail-recursive method? Why or why not? class SomeThing {     public static void main(String args[]) {         System.out.println(foo(6));     }     private static int foo(int input) {         if (input <= 0) {             throw new RuntimeException("invalid input");         }         else if (input == 1) {             return 1;         }         else if (input %...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
Mention the concepts analyzed from the below given program and write the output. [0.5 M] class...
Mention the concepts analyzed from the below given program and write the output. [0.5 M] class Test { int a, b; Test(int i, int j) { a = i; b = j; } void meth(Test s) { s.a *= 2; s.b /= 2; } } class demo { public static void main(String args[]) { Test ob = new Test(15, 20); System.out.println("Before call: " + ob.a + " " + ob.b); ob.meth(ob); System.out.println("After call: " + ob.a + " " +...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT