# Program to check a number is divisible by 5 or not

Last Updated : 26 Feb, 2024

Given number N, the task is to check whether it is divisible by 5. Print “Yes” if N is divisible by 5; otherwise, print “No“.

Examples:

Input: N = 155
Output: Yes
Explanation: If we divide 155 by 5, we get the remainder as 0, therefore 155 is divisible by 5.

Input: N = 17
Output: No
Explanation: If we divide 17 by 5, we get the remainder by 2, therefore 17 is divisible by 5.

## Program to check a number is divisible by 5 or not using Divisibility of 5:

If observed carefully, all the multiples of 5 have either 0 or 5 as its last digit hence to check the divisibility, below two cases can be formed:

• if (Last digit of N == 0 or last digit of N == 5): N is divisible by 5
• Else N is not divisible by 5.

Note: To get the last Digit we can use the formula last Digit = N%10

Step-by-step approach:

• Find the last digit of N by performing N % 10.
• If the last digit is 5 or 0, then N is divisible by 5.
• Else, N is not divisible by 5.

Below is the implementation of the above approach:

## C++

 `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``int` `N = 17;`   `    ``// Extract the last digit using N % 10` `    ``int` `lastDigit = N % 10;`   `    ``// Check for last digit` `    ``if` `(lastDigit == 0 || lastDigit == 5) {` `        ``cout << N << ``" is divisible by 5"``;` `    ``}` `    ``else` `{` `        ``cout << N << ``" is not divisible by 5"``;` `    ``}` `}`

## Java

 `public` `class` `Main {` `    ``public` `static` `void` `main(String[] args) {` `        ``int` `N = ``17``;`   `        ``// Extract the last digit using N % 10` `        ``int` `lastDigit = N % ``10``;`   `        ``// Check for last digit` `        ``if` `(lastDigit == ``0` `|| lastDigit == ``5``) {` `            ``System.out.println(N + ``" is divisible by 5"``);` `        ``} ``else` `{` `            ``System.out.println(N + ``" is not divisible by 5"``);` `        ``}` `    ``}` `}`   `// This code is contributed by shivamgupta0987654321`

## Python3

 `class` `GFG:` `    ``@staticmethod` `    ``def` `main():` `        ``N ``=` `17`   `        ``# Extract the last digit using N % 10` `        ``last_digit ``=` `N ``%` `10`   `        ``# Check for the last digit` `        ``if` `last_digit ``=``=` `0` `or` `last_digit ``=``=` `5``:` `            ``print``(``str``(N) ``+` `" is divisible by 5"``)` `        ``else``:` `            ``print``(``str``(N) ``+` `" is not divisible by 5"``)`   `# Call the main method` `GFG.main()`

## C#

 `using` `System;`   `public` `class` `GFG {`   `    ``static` `public` `void` `Main()` `    ``{`   `        ``int` `N = 17;`   `        ``// Extract the last digit using N % 10` `        ``int` `lastDigit = N % 10;`   `        ``// Check for the last digit` `        ``if` `(lastDigit == 0 || lastDigit == 5) {` `            ``Console.WriteLine(N + ``" is divisible by 5"``);` `        ``}` `        ``else` `{` `            ``Console.WriteLine(N + ``" is not divisible by 5"``);` `        ``}` `    ``}` `}`

## Javascript

 `let N = 17;`   `// Extract the last digit using N % 10` `let lastDigit = N % 10;`   `// Check for last digit` `if` `(lastDigit === 0 || lastDigit === 5) {` `    ``console.log(N + ``" is divisible by 5"``);` `} ``else` `{` `    ``console.log(N + ``" is not divisible by 5"``);` `}`

Output

```17 is not divisible by 5

```

Time Complexity: O(1)
Auxiliary Space: O(1)

## Program to check a number is divisible by 5 or not using Modulo Operator (%):

When you divide one integer by another, the remainder can be 0 (indicating exact divisibility) or a non-zero value (indicating that there is a remainder). If the remainder is 0, it means that the dividend is divisible by the divisor.

Step-by-step approach:

• Check If `N % 5` equals 0, then `N` is divisible by `5`.
• Else, `N` is not divisible by `5`.

Below is the implementation of the approach:

## C++

 `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``int` `N = 155;` `    ``// Divisibility Check using Modulo Operator` `    ``if` `(N % 5 == 0) {` `        ``cout << N << ``" is divisible by 5"``;` `    ``}` `    ``else` `{` `        ``cout << N << ``" is not divisible by 5"``;` `    ``}` `}`

## Java

 `/*code by flutterfly*/` `public` `class` `Main {` `    ``public` `static` `void` `main(String[] args) {` `        ``int` `N = ``155``;` `         ``// Divisibility Check using Modulo Operator` `        ``if` `(N % ``5` `== ``0``) {` `            ``System.out.println(N + ``" is divisible by 5"``);` `        ``} ``else` `{` `            ``System.out.println(N + ``" is not divisible by 5"``);` `        ``}` `    ``}` `}`

## Python3

 `# code by flutterfly` `def` `main():` `    ``N ``=` `155`   `    ``# Divisibility Check using Modulo Operator` `    ``if` `N ``%` `5` `=``=` `0``:` `        ``print``(f``"{N} is divisible by 5"``)` `    ``else``:` `        ``print``(f``"{N} is not divisible by 5"``)`   `# Run the main function` `if` `__name__ ``=``=` `"__main__"``:` `    ``main()`

## C#

 `//code by flutterfly` `using` `System;`   `class` `MainClass` `{` `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``int` `N = 155;`   `        ``// Divisibility Check using Modulo Operator` `        ``if` `(N % 5 == 0)` `        ``{` `            ``Console.WriteLine(\$``"{N} is divisible by 5"``);` `        ``}` `        ``else` `        ``{` `            ``Console.WriteLine(\$``"{N} is not divisible by 5"``);` `        ``}` `    ``}` `}`

## Javascript

 `const N = 155;`   `// Divisibility Check using Modulo Operator` `if` `(N % 5 === 0) {` `    ``console.log(`\${N} is divisible by 5`);` `} ``else` `{` `    ``console.log(`\${N} is not divisible by 5`);` `}`

Output

```155 is divisible by 5

```

Time Complexity: O(1)
Auxiliary Space: O(1)

Previous
Next