- Related Questions & Answers
- Find N digits number which is divisible by D in C++
- Sum which is divisible by n in JavaScript
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Find elements of an array which are divisible by N using STL in C++
- Check if any permutation of a number is divisible by 3 and is Palindromic in Python
- Check if a large number is divisible by 3 or not in C++
- Check if a large number is divisible by 3 or not in java
- Check if a large number is divisible by 2, 3 and 5 or not in C++
- Find the number closest to n and divisible by m in C++
- Sum of first N natural numbers which are divisible by X or Y
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- Check if any permutation of a large number is divisible by 8 in Python
- Check if a number is divisible by 23 or not in C++
- Check if a number is divisible by 41 or not in C++
- Find if nCr is divisible by the given prime 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

Suppose we have a number n, and we have to find the permutation of this number, that is divisible by 3, but not divisible by 6. If no such value can be made, then return -1. For example, if n is 336, then the output can be 363.

As we know a number is divisible by 6 means it is divisible by 3 and 2. So each even number that is divisible by 3, will be divisible by 6. If we interchange the digits of a number which is divisible by 3 and also even, to make it odd, it will be the result.

#include<iostream> #include<cmath> using namespace std; int findNumber(int n) { int digit_count = ceil(log10(n)); for (int i = 0; i < digit_count; i++) { if (n % 2 != 0) { return n; } else { n = (n / 10) + (n % 10) * pow(10, digit_count - i - 1); continue; } } return -1; } int main() { int n = 132; cout <<"The permutation of "<<n << " that is divisible by 3 but not by 6 is:"<< findNumber(n); }

The permutation of 132 that is divisible by 3 but not by 6 is:213

Advertisements