
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find permutation of n which is divisible by 3 but not divisible by 6 in C++
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.
Example
#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); }
Output
The permutation of 132 that is divisible by 3 but not by 6 is:213
- Related Articles
- Given an example of a number which is divisible by(i) 2 but not by 4.(ii) 3 but not by 6.(iii) 4 but not by 8.(iv) both 4 and 8 but not 32.
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- If a number is divisible by 3 need it to be tested for 9? Justify your answer by stating any 2 numbers which are divisible by 3 but not by 9.
- Sum which is divisible by n in JavaScript
- For any positive integer n, prove that $n^3-n$ is divisible by 6.
- Is 297144 divisible by 6?
- Find N digits number which is divisible by D in C++
- Check whether 974 is divisible by 3 or not.
- 18 is divisible by both 2 and 3 . It is also divisible by \( 2 \times 3=6 \). Similarly, a number is divisible by both 4 and 6. Can we say that the number must also be divisible by \( 4 \times 6=24 \) ? If not, give an example to justify your answer.
- Number of substrings divisible by 8 and not by 3 in C++
- Check if any permutation of a number is divisible by 3 and is Palindromic in Python
- Find the greatest 3 digit number which is divisible by the number 4 ,5 and 6.
- Which is the biggest number by which 6 , 9 and 12 divisible
- If a number is divisible by 3 it must be divisible by 9. (True/False)
- Check if 74526 is divisible by 3.

Advertisements