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 <iostream>
using namespace std;
int main()
{
int N = 17;
int lastDigit = N % 10;
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 ;
int lastDigit = N % 10 ;
if (lastDigit == 0 || lastDigit == 5 ) {
System.out.println(N + " is divisible by 5" );
} else {
System.out.println(N + " is not divisible by 5" );
}
}
}
|
Python3
class GFG:
@staticmethod
def main():
N = 17
last_digit = N % 10
if last_digit = = 0 or last_digit = = 5 :
print ( str (N) + " is divisible by 5" )
else :
print ( str (N) + " is not divisible by 5" )
GFG.main()
|
C#
using System;
public class GFG {
static public void Main()
{
int N = 17;
int lastDigit = N % 10;
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;
let lastDigit = N % 10;
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 <iostream>
using namespace std;
int main()
{
int N = 155;
if (N % 5 == 0) {
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 = 155 ;
if (N % 5 == 0 ) {
System.out.println(N + " is divisible by 5" );
} else {
System.out.println(N + " is not divisible by 5" );
}
}
}
|
Python3
def main():
N = 155
if N % 5 = = 0 :
print (f "{N} is divisible by 5" )
else :
print (f "{N} is not divisible by 5" )
if __name__ = = "__main__" :
main()
|
C#
using System;
class MainClass
{
public static void Main( string [] args)
{
int N = 155;
if (N % 5 == 0)
{
Console.WriteLine($ "{N} is divisible by 5" );
}
else
{
Console.WriteLine($ "{N} is not divisible by 5" );
}
}
}
|
Javascript
const N = 155;
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)
Share your thoughts in the comments
Please Login to comment...