In the previous article, we have discussed Java Program to Check Strong Number
In this article we will see how to check a number is a Tcefrep number or not in Java programming language.
Java Program to Check Tcefrep Number
Before going to the actual program, let’s first know what is a Tcefrep Number.
Tcefrep Number is a number whose reverse is equal to the sum of all the divisors of it.
Example: Number = 6 All divisors of 6 : 1, 2 and 3 Sum of all divisors of 6 : 1+2+3 = 6 Reverse of 6 = 6 Now, Reverse of 6 is Equal to the sum of all divisors of 6. So, it is a Tcefrep number.
Let’s seedifferent ways to check Tcefrep number.
Approach:
- Declare or take input of a number.
- Then find the reverse of the number.
- Then find the sum of all it’s divisors.
- Check if the sum of all it’s divisors is equal to the reverse of the number, then it is a Tcefrep number.
Method-1: Java Program to Check Tcefrep Number By Using Static Value
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//A number declared
int number = 498906;
//Assigning the actualnumber to 'temp' variable
int temp=number;
//finding reverse of the number
int reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.out.println("Reverse of number : "+reverse);
// An integer variable declared
// to hold the sum of all proper divisors
//Initialvalueassigned with 0
int result = 0;
// finding all divisors which divides the number
for (int i = 2; i<= Math.sqrt(temp); i++)
{
// if number is divisible by 'i'
if (temp % i == 0)
{
// Here it checks if both divisors are same
//then it adds only once else add both
if (i == (temp / i))
result = result + i;
else
result = result + (i + temp / i);
}
}
//Adds 1 to the result
//as 1 is also a divisor of the number
result=result+1;
System.out.println("Sum of all divisors : "+result);
//Checking Tcefrep number
if(reverse==result)
System.out.println("Tcefrep Number");
else
System.out.println("Not a Tcefrep Number");
}
}
Output: Reverse of number : 609894 Sum of all divisors : 609894 Tcefrep Number
Method-2: Java Program to Check Tcefrep Number By User Input Value
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner sc=new Scanner(System.in);
//Taking the number input from the user
int number = sc.nextInt();
//Assigning the actualnumber to 'temp' variable
int temp=number;
//finding reverse of the number
int reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.out.println("Reverse of number : "+reverse);
// An integer variable declared
// to hold the sum of all proper divisors
//Initialvalueassigned with 0
int result = 0;
// finding all divisors which divides the number
for (int i = 2; i<= Math.sqrt(temp); i++)
{
// if number is divisible by 'i'
if (temp % i == 0)
{
// Here it checks if both divisors are same
//then it adds only once else add both
if (i == (temp / i))
result = result + i;
else
result = result + (i + temp / i);
}
}
//Adds 1 to the result
//as 1 is also a divisor of the number
result=result+1;
System.out.println("Sum of all divisors : "+result);
//Checking Tcefrep number
if(reverse==result)
System.out.println("Tcefrep Number");
else
System.out.println("Not a Tcefrep Number");
}
}
Output: Reverse of number : 6 Sum of all divisors : 6 Tcefrep Number
Method-3: Java Program to Check Tcefrep Number By User Defined Method
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//A number declared
int number = 498906;
//User defined Method
//numberCheck() method called to check Tcefrep number
numberCheck(number);
}
//Method to Reverse the Number
static int reverseNumber(int number)
{
int reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
return reverse;
}
// Method to calculate the sum of all proper divisors
static int DivisorSum(int number)
{
// An integer variable declared
// to hold the sum of all proper divisors
//Initialvalueassigned with 0
int result = 0;
// finding all divisors which divides the number
for (int i = 2; i<= Math.sqrt(number); i++)
{
// if number is divisible by 'i'
if (number % i == 0)
{
// Here it checks if both divisors are same
//then it adds only once else add both
if (i == (number / i))
result = result + i;
else
result = result + (i + number / i);
}
}
//Adds 1 to the result
//as 1 is also a divisor of the number
result=result+1;
return (result);
}
//User defined Method to check Tcefrep number
static void numberCheck(int number)
{
if(DivisorSum(number) == reverseNumber(number))
System.out.println("Tcefrep Number");
else
System.out.println("Not a Tcefrep Number");
}
}
Output: Tcefrep Number
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples.
Related Java Programs: