3. Check Divisibility by 3

Easy ⏱️ 7 min

Problem Statement

Given an integer value, check the given input is divisible by 3. If it is divisible print the message "The number is divisible by 3". If it is not divisible print the message "The number is not divisible by 3 and gives a remainder _".

Input Format:
Enter an integer as a input
Output Format:
Follow the format as sample output
Constraints:
1 <= INPUT <= 10^15
Sample Input 1:
25
Sample Output 1:
The number is not divisible by 3 and gives a remainder 1
Sample Input 2:
54653
Sample Output 2:
The number is not divisible by 3 and gives a remainder 2
🧱 C++ Else-If Statements Guide
🔗 What is Else-If?

else if allows you to check multiple conditions in sequence.

Syntax:

if (condition1) { /* block 1 */ }
else if (condition2) { /* block 2 */ }
else { /* block 3 */ }

How it works:

  • First, condition1 is checked
  • If TRUE → Execute block 1 and skip the rest
  • If FALSE → Check condition2
  • If condition2 is TRUE → Execute block 2
  • If all conditions are FALSE → Execute else block

✅ Only ONE block will execute (the first condition that's true).

🆚 If vs Else-If: What's the Difference?

Multiple if statements (independent):

if (a > 0) { /* runs if true */ }
if (a > 10) { /* also runs if true */ }

Both conditions are checked, and both blocks can run.

Else-if ladder (mutually exclusive):

if (a > 10) { /* runs if true */ }
else if (a > 0) { /* only runs if first is false */ }

✅ Only one block executes. Once a condition is true, the rest are skipped.

💡 Use else-if when: You have mutually exclusive conditions (only one should execute).

💡 Use separate if when: You want to check independent conditions that can both be true.

➗ Divisibility Check with Modulo

To check if a number is divisible by 3:

if (a % 3 == 0) → Number is divisible (remainder is 0)

if (a % 3 != 0) → Number is NOT divisible (remainder is 1 or 2)

Getting the remainder:

long long remain = a % 3;

Examples:

  • 9 % 3 = 0 → Divisible ✅
  • 10 % 3 = 1 → Not divisible, remainder 1
  • 25 % 3 = 1 → Not divisible, remainder 1
  • 54653 % 3 = 2 → Not divisible, remainder 2
🔍 Code Flow Analysis

Step-by-step with a = 25:

  1. Read input: a = 25
  2. Check: if (25 % 3 == 0)25 % 3 = 1, so 1 == 0 is FALSE
  3. Skip first block, go to else if
  4. Check: else if (25 % 3 != 0)1 != 0 is TRUE
  5. Execute: long long remain = 25 % 3;remain = 1
  6. Print: "The number is not divisible by 3 and gives a remainder 1"

Step-by-step with a = 9:

  1. Read input: a = 9
  2. Check: if (9 % 3 == 0)9 % 3 = 0, so 0 == 0 is TRUE
  3. Execute first block: Print "The number is divisible by 3"
  4. Skip else-if block entirely ✅
🎨 Mixing Braces and No-Braces Style

Notice in this code we mix both styles:

Without braces (single statement):

if (a % 3 == 0)
  cout << "The number is divisible by 3";

With braces (multiple statements):

else if (a % 3 != 0) {
  long long remain = a % 3;
  cout << "..." << remain;
}

✅ This is perfectly valid! The first block has ONE statement (no braces needed), but the second block has TWO statements (braces required).

💡 Key rule: You can mix styles in the same if-else chain. Each block follows its own rule based on statement count.

💡 Tip: Use else-if when checking mutually exclusive conditions to make your code more efficient!

Solution

#include <iostream>

using namespace std;

int main() {
    long long a;
    cin >> a;
    
    if (a % 3 == 0)
        cout << "The number is divisible by 3";
    else if (a % 3 != 0) {
        long long remain = a % 3;
        cout << "The number is not divisible by 3 and gives a remainder " << remain;
    }
    
    return 0;
}