
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Divisibility of a Large Number by 2, 3, and 5 in C++
Here we will see how to check a number is divisible by 2, 3 and 5 or not. In this case the number is very large number. So we put the number as string.
A number will be divisible by 2, 3 and 5 if that number is divisible by LCM of 2,3 and 5. So the LCM of 2, 3, 5 is 30. We have to check the number is divisible by 30 or not. A number is divisible by 30 when it is divisible by 10 (last digit is 0) and divisible by 3 (sum of all digits is divisible by 3)
Example
#include <bits/stdc++.h> using namespace std; bool isDiv30(string num){ int n = num.length(); if(num[n - 1] != '0') return false; long sum = accumulate(begin(num), end(num), 0) - '0' * n; if(sum % 3 == 0) return true; return false; } int main() { string num = "353730746028908374890"; if(isDiv30(num)){ cout << "Divisible"; } else { cout << "Not Divisible"; } }
Output
Divisible
Advertisements