Apex Divisibility Check Function

Sometimes, you just need to check if a number plays nicely with certain rules — like whether it’s divisible by specific values. In this case, we’re checking if a number is divisible by both 7 and 11.

This may sound like a simple math problem (and it is!), but it’s also a perfect example to practice conditional logic, modular arithmetic, and clean Apex coding.

In this blog post, we’ll walk through how to build a quick method in Apex that checks if a number is divisible by both 7 and 11 and returns a true or false result accordingly.

🧠 What Are We Trying to Do?

 

Let’s say we have a number — for example, 77. We want to check:

  • Is 77 divisible by 7? ✅

  • Is 77 divisible by 11? ✅

  • If both answers are yes → return true

  • If not → return false

This kind of logic can be useful in:

  • Validations

  • Business rules

  • Custom workflows

  • Fun practice exercises to improve your coding logic

🔍 Code Explanation

 
public with sharing class ApexUseCaseEight {
    
    public static Boolean divisibleBySevAndElev(Integer numberToCheck){

We define a class called ApexUseCaseEight and inside it, a static method named divisibleBySevAndElev. It takes one input: numberToCheck, which is the number we want to evaluate.

 
        if(numberToCheck != null){

First, we check that the input is not null — always a good practice to avoid errors.

 
            if(Math.mod(numberToCheck, 7) == 0 && Math.mod(numberToCheck, 11) == 0){
                return true;
            }

Here’s where the magic happens:

  • We use Math.mod() to check if the remainder is 0 when dividing the number by 7 and 11.

  • If both conditions are true, we return true.

 
        }
        return false;
    }
}

If the input is null or fails either check, we return false.

✅ Why This Is Useful

 

This method may look simple, but it teaches you some key concepts:

  • How to use the modulus function (Math.mod)

  • How to apply multiple conditions using logical operators (&&)

  • How to handle null input safely

You can adapt this to check other conditions — like divisibility by 3 and 5 (FizzBuzz, anyone?), or plug it into validation logic.

🧾 Final Code Snippet

 
public with sharing class ApexUseCaseEight {
    
    public static Boolean divisibleBySevAndElev(Integer numberToCheck){
        if(numberToCheck != null){
            if(Math.mod(numberToCheck, 7) == 0 && Math.mod(numberToCheck, 11) == 0){
                return true;
            }
        }
        return false;
    }
}

🎥 Watch It in Action!

Want to see this in action? Watch my short video where I run this logic in the Salesforce Developer Console and explain step-by-step what’s happening.

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)