C++ Program to Check If a Number is Divisible by 5 and 11

Howdy readers, today you will learn how to write a C++ program to check if a number is divisible by 5 and 11.

The below tutorial prompts the user to enter an integer, then it checks whether the entered integer is divisible by 5 and 11 or not using the following methods:

  1. Using If-Else Statement
  2. Using Ternary Operator

So, without any delay, let’s begin this tutorial.

C++ Program to Check If a Number is Divisible by 5 and 11

C++ Program

// C++ Program to Check If a Number is Divisible by 5 and 11
#include <iostream>
using namespace std;

int main(){
    int num;
    
    // Taking input
    cout << "Enter a number: ";
    cin >> num;
    
    // Divisible by 5 and 11
    if (num % 5 == 0 && num % 11 == 0){
        cout << num << " is divisible by both 5 and 11." << endl;
    }
    else{
        cout << num << " is not divisible by 5 and 11." << endl;
    }
    
    return 0;
}

Output

Enter a number: 440
440 is divisible by both 5 and 11.
C++ Program to Check If a Number is Divisible by 5 and 11

Explanation

    int num;

We have declared an int data type variable named num in this program.

    cout << "Enter a number: ";
    cin >> num;

Now, the user is asked to enter an integer to check whether it is divisible by 5 and 11 or not. The entered integer gets stored in the num named variable.

    if (num % 5 == 0 && num % 11 == 0){
        cout << num << " is divisible by both 5 and 11." << endl;
    }
    else{
        cout << num << " is not divisible by 5 and 11." << endl;
    }

We used an if-else statement to check whether the entered integer is divisible by both 5 and 11 or not.

If the number is divisible by both 5 and 11, then we print the entered number is divisible by both 5 and 11.

Otherwise, we print the entered number is not divisible by 5 and 11.

C++ Program to Check If a Number is Divisible by 5 and 11 Using Ternary Operator

C++ Program

// C++ Program to Check If a Number is Divisible by 5 and 11 Using Ternary Operator
#include <iostream>
using namespace std;

int main(){
    int num;
    
    // Taking input
    cout << "Enter an integer: ";
    cin >> num;
    
    // Ternary Operator
    (num % 5 == 0 && num % 11 == 0) ? cout << num << " is divisible by 5 and 11." << endl : cout << num << " is not divisible by 5 and 11." << endl;
    
    return 0;
}

Output 1

Enter an integer: 110
110 is divisible by 5 and 11.

Output 2

Enter an integer: 210
210 is not divisible by 5 and 11.
C++ Program to Check If a Number is Divisible by 5 and 11 Using Ternary Operator

Explanation

    (num % 5 == 0 && num % 11 == 0) ? cout << num << " is divisible by 5 and 11." << endl : cout << num << " is not divisible by 5 and 11." << endl;

The syntax of the ternary operator is: Condition ? expression1 : expression2.

If the condition is evaluated boolean True, then expression1 is executed otherwise expression1 is executed.

Similarly, here if the condition (num % 5 == 0 && num % 11 == 0) is True, then expression1 (cout << num << ” is divisible by 5 and 11.” << endl) is executed.

Otherwise, if the condition is False, then expression2 (cout << num << ” is not divisible by 5 and 11.” << endl;) is executed.

Conclusion

Today you learned how to write a C++ program to check if a number is divisible by 5 and 11.

If you have any questions related to this tutorial, comment down your queries in the comment section.

Thanks for reading.

Happy Coding!!

Also Read:

Leave a Comment