Refactorable number in C++


We are given an integer type value, let's say, number. The task is to check whether the given number is Refactorable or not. If yes print that the number is a refactorable number else print not possible.

What is a Refactorable Number?

A number is refactorable when it is divisible by its total count of factors available. For example, number 9 is refactorable as it has a total number of factors i.e. 3(1, 3, 9) and 9 is divisible by 3 hence its a refactorable number.

Let us see various input output scenarios for this −

Input − int number = 9

Output − It is a Refactorable number

Explanation − A number is refactorable when it is divisible by its total count of factors available. We are given a number 9, which is refactorable as it has a total number of factors i.e. 3(1, 3, 9) and 9 is divisible by 3 hence its a refactorable number.

Input − int number = 10

Output − It isn't a Refactorable number

Explanation − A number is refactorable when it is divisible by its total count of factors available. We are given a number 10, which isn't refactorable as it has a total number of factors i.e. 4(1, 2, 5, 10) and 10 isn’t divisible by 4 hence it is not a refactorable number

Approach used in the below program is as follows

  • Input a variable of integer type, let’s say, number.

  • Pass the data to the function check_Refactorable(int number) of the bool type.

  • Inside the function check_Refactorable(int number)

    • Declare an integer type variable as count to 0.

    • Start loop FOR from i to 1 till i is less than sqrt(number). Inside the loop, check IF number % i = 0 then check IF number / i = i then pre increment the count by 1.

    • ELSE, set the count as count + 2.

    • Return number % count == 0

  • Print the result.

Example

#include <bits/stdc++.h>
using namespace std;
bool check_Refactorable(int number){
   int count = 0;
   for (int i = 1; i <= sqrt(number); ++i){
      if(number % i==0){
         if(number / i == i){
            ++count;
         }
         else{
            count += 2;
         }
      }
   }
   return number % count == 0;
}
int main(){
   int number = 9;
   if(check_Refactorable(number) == 1){
      cout<<"It is a Refactorable number";
   }
   else{
      cout<<"It isn't a Refactorable number";
   }
   return 0;
}

Output

If we run the above code it will generate the following Output

It is a Refactorable number

Updated on: 03-Nov-2021

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements