id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

srp4379@lausd.net 1-logicbasics > isDivisibleBy5andNOTby10
prev  |  next  |  chance

public boolean isDivisibleBy5andNOTby10(int n)

Given a number (int), return true if it is
divisible by 5 AND is NOT divisible by 10, false otherwise.

The way to check whether a number is NOT divisible by
by another number divisor is as follows:

number % divisor != 0

SO:
n % 10 != 0 will check for numbers that are NOT divisible by 10.
n % 5 != 0 will check for numbers that are NOT divisible by 5.
n % 2 != 0 will check for numbers that are NOT divisible by 2.

That said, there are TWO ways to craft a correct solution:

#1:
public boolean isDivisibleBy5andNOTby10(int n) {
  boolean divBy5 = false;
  if (n % 5 == 0) {
    divBy5 = true;
  }

  boolean notDivBy10 = false;
  if (n % 10 != 0) {
    notDivBy10 = true;
  }

  boolean divBy5andNot10 = false;
  if (divBy5 && notDivBy10) {
    divBy5andNot10 = true;
  }
  return divBy5andNot10;
}
#2:
public boolean isDivisibleBy5andNOTby10(int n) {
  boolean divBy5 = false;
  if (n % 5 == 0) {
    divBy5 = true;
  }

  boolean divBy10 = false; // !!!!!
  if (n % 10 == 0) {
    divBy10 = true;
  }

  boolean divBy5andNot10 = false;
  if (divBy5 && !divBy10) {  // !!!!!
    divBy5andNot10 = true;
  }
  return divBy5andNot10;
}

isDivisibleBy5andNOTby10(5) → true
isDivisibleBy5andNOTby10(10) → false
isDivisibleBy5andNOTby10(15) → true

...Save, Compile, Run (ctrl-enter)

public boolean isDivisibleBy5andNOTby10(int n) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 1

Copyright Nick Parlante 2017 - privacy