- Related Questions & Answers
- Check if a large number is divisibility by 15 in C++
- To check divisibility of any large number by 9 in java
- Divisible by 37 for large numbers in C++ Program
- Divisibility by 64 with removal of bits allowed in C++ Program
- Check if a large number is divisible by 20 in C++
- Program to find remainder when large number is divided by 11 in C++
- Program to find remainder when large number is divided by r in C++
- Divide large number represented as string in C++ Program
- Check if a large number is divisible by 11 or not in C++
- Check if a large number is divisible by 25 or not in C++
- Check if a large number is divisible by 3 or not in C++
- Check if a large number is divisible by 5 or not in C++
- Check if a large number is divisible by 75 or not in C++
- Check if a large number is divisible by 8 or not in C++
- Check if a large number is divisible by 9 or not in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this tutorial, we are going to write a program that checks whether the given large number in string format is divisible by 12 or not.

We are going to use a little bit of math to solve this problem. If the number is divisible by 3 and 4, then the number will divisible by 12.

A number is divisible by 3 if the sum of its digits is divisible by 3.

A number is divisible by 4 if the last two digits of the number are divisible by 4.

We are going to utilize the above statements and complete the program.

Let's see the code.

#include <bits/stdc++.h> using namespace std; bool isNumberDivisibleBy12(string num) { if (num.length() >= 3) { int last_digit = (int)num[num.length() - 1]; if (last_digit % 2 != 0) { return 0; } int second_last_digit = (int)num[num.length() - 2]; int sum = 0; for (int i = 0; i < num.length(); i++) { sum += num[i]; } return (sum % 3 == 0 && (second_last_digit * 10 + last_digit) % 4 == 0); } else { int number_as_int = stoi(num); return number_as_int % 12 == 0; } } int main() { string num = "1212121212121212121212121212"; if (isNumberDivisibleBy12(num)) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; }

If you execute the above program, then you will get the following result.

Yes

If you have any queries in the tutorial, mention them in the comment section.

Advertisements