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 + "." ); } } |
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 + "."); } }
Get Answers For Free
Most questions answered within 1 hours.