How To Check Whether a Number is Tcefrep Number or Not in Java?


A number is said to be a Tcefrep number, if the reverse of that number is equal to the sum of the proper divisors of the original number.

For more clarification, we find the sum of the divisors of a number and find the reverse of the original number. If both the sum value and the reverse of the given number are the same then we can say that the given number is a Tcefrep number.

Some examples of Tcefrep numbers are: 6, 498906, 20671542 ... etc.

In this article, we will see how to check if a number is a Tcefrep number by using Java programming language.

To show you some instances

Instance-1

Input number is 498906.

Let’s check it by using the logic of Tcefrep number.

Reverse of the given number= 609894.

Proper divisors of given number 498906 are= 1, 2, 3, 6, 9, 18, 27, 54, 9239, 18478, 27717, 55434, 83151, 166302, 249453.

Sum of above numbers = 609894

As we notice here both the calculated sum value and reverse of the original number are same.

Hence, 498906 is a Tcefrep number.

Instance-2

Input number is 20671542.

Let’s check it by using the logic of Tcefrep number.

Reverse of the given number= 24517602.

Proper divisors of given number 498906 are= 1 ,2 ,3 ,6 ,9 ,18 ,113 ,226 ,339 ,678 ,1017 ,2034 ,10163 ,20326 ,30489 ,60978 ,91467 ,182934 ,1148419 ,2296838 ,3445257 ,6890514 ,10335771.

Sum of above numbers = 24517602

As we notice here both the calculated sum value and reverse of the original number are same.

Hence, 20671542 is a Tcefrep number.

Instance-3

Input number is 12343233.

Let’s check it by using the logic of Tcefrep number.

Reverse of the given number= 33234321.

Proper divisors of given number 498906 are= 1 ,3 ,7 ,21 ,587773 ,1763319 ,4114411.

Sum of above numbers = 18808768

As we notice here both the calculated sum value and reverse of the original number are not same.

Hence, 12343233 is not a Tcefrep number.

Algorithm

Step-1 - Get the input number either by static input or by user input.

Step-2 - Find the reverse of the original number.

Step-3 - Now find all the possible proper divisors of the original number and calculate the sum of it.

Step-4 - Then compare both the reverse value with the calculated sum value.

Step-5 - If both the values are same, then the input number is called as Tcefrep number else not.

Syntax

To get the square root of a number we have inbuilt sqrt() method in the Math class of java.lang package.

Following is the syntax to get square root of any number by using the method -

double squareRoot = Math.sqrt(input_vale)

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value with User Defined Method

  • By Using User Input Value with User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value with User Defined Method

In this approach, we declare a variable and initialize it with a positive number and pass this number as parameter in a user defined method, then inside the method by using the algorithm we can check whether the number is a Tcefrep number or not.

Example

public class Main {
   
   //main method
   public static void main(String[] args) {
      
      //declare a variable and store a value by static input method
      int inputNumber = 20671542;

      
      //call the user-defined method to check for Tcefrep number
      if (checkTcefrep(inputNumber))
         System.out.print(inputNumber+" is a Tcefrep number.");
      else
         System.out.print(inputNumber+" is not a Tcefrep number.");
   }

   
   //user-defined method to find the reverse of input number
   static int reverseNumber(int n) {
      
      //declare a variable to store the reverse number
      int rev = 0;
      
      //continue the loop till n becomes zero
      while(n > 0) {
         rev = rev * 10 + n % 10;
         n = n / 10;
      }
      
      //return the reverse number
      return rev;
   }
   
   // user-defined method to find all the proper divisors and return the sum value
   static int properDivisorsSum(int n) {
      
      // Declare a variable to store the sum value
      int sum = 0;

      // Loop to find the divisors
      for (int i = 2; i<= Math.sqrt(n); i++) {
         if (n % i == 0) {
            if (i == (n / i))
               sum += i;
            else
               sum += (i + n / i);
         }
      }

      // We find divisors excluding 1 so we add one with final sum value
      return (sum + 1);
   }

   //user-defined method to check the Tcefrep number
   static boolean checkTcefrep(int n) {
      
      //return true if condition satisfied
      
      //here we are calling user defined method properDivisorsSum()
      
      //for sum of all proper divisors and calling reverseNumber() method
      
      //to get the reverse value of original number
      
      //and returning true if the result of both methods are same else return false
      return properDivisorsSum(n) == reverseNumber(n);
   } 
}

Output

20671542 is a Tcefrep number.

Approach-2: By Using User Input Value with User Defined Method

In this approach, we declare a variable and take a positive number as user input and pass this number as parameter in a user defined method, then inside the method by using the algorithm we can check whether the number is a Tcefrep number or not.

Example

import java.util.*;
public class Main {
   
   //main method
   public static void main(String[] args) {
      
      //create object of Scanner class
      Scanner sc = new Scanner(System.in);
      
      //Ask user to enter a number
      System.out.print("Enter a number: ");
      
      //declare a variable and take the value by user input
      int inputNumber = sc.nextInt();

      //call the user-defined method to check for Tcefrep number
      if (checkTcefrep(inputNumber))
         System.out.print(inputNumber+" is a Tcefrep number.");
      else
         System.out.print(inputNumber+" is not a Tcefrep number.");
   }

   //user-defined method to find the reverse of input number
   static int reverseNumber(int n) {
      
      //declare a variable to store the reverse number
      int rev = 0;
      
      //continue the loop till n becomes zero
      while(n > 0) {
         rev = rev * 10 + n % 10;
         n = n / 10;
      }
      
      //return the reverse number
      return rev;
   }

   // user-defined method to find all the proper divisors and return the sum value
   static int properDivisorsSum(int n) {
      // Declare a variable to store the sum value
      int sum = 0;

      // Loop to find the divisors
      for (int i = 2; i<= Math.sqrt(n); i++) {
         if (n % i == 0) {
            if (i == (n / i))
               sum += i;
            else
               sum += (i + n / i);
         }
      }

      // We find divisors excluding 1 so we add one with final sum value
      return (sum + 1);
   }
   
   //user-defined method to check the Tcefrep number
   static boolean checkTcefrep(int n) {
      
      //return true if condition satisfied
      
      //here we are calling user defined method properDivisorsSum()
      
      //for sum of all proper divisors and calling reverseNumber() method
      
      //to get the reverse value of original number
      
      //and returning true if the result of both methods are same else return false
      return properDivisorsSum(n) == reverseNumber(n);
   }
}

Output

Enter a number: 6
6 is a Tcefrep number.

In this article, we explored how to check a number whether it is a Tcefrep number or not in Java by using different approaches

Updated on: 09-Dec-2022

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements