VOOZH about

URL: https://www.geeksforgeeks.org/android/program-to-check-a-number-is-divisible-by-5-or-not/

⇱ Program to check a number is divisible by 5 or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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:


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:


Output
155 is divisible by 5

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

Comment
Article Tags:
Article Tags:

Explore