Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Check if a large number is divisible by 3 or not in C++
Here we will see how to check a number is divisible by 3 or not. In this case the number is very large number. So we put the number as string.
A number will be divisible by 3, if the sum of digits is divisible by 3.
Example
#include <bits/stdc++.h>
using namespace std;
bool isDiv3(string num){
int n = num.length();
long sum = accumulate(begin(num), end(num), 0) - '0' * n;
if(sum % 3 == 0)
return true;
return false;
}
int main() {
string num = "3635883959606670431112222";
if(isDiv3(num)){
cout << "Divisible";
} else {
cout << "Not Divisible";
}
}
Output
Divisible
Advertisements
