0% found this document useful (0 votes)
11 views10 pages

Dudeney Number Checker in Java

The document contains multiple Java programs that demonstrate various mathematical concepts and number classifications, including converting hexadecimal to decimal, identifying evil numbers, checking for pronic numbers, palindromes, disarium numbers, Dudeney numbers, calculating LCM and HCF, and determining perfect and Armstrong numbers. Each program is structured with a main method that executes the respective functionality and prints the results. The implementations utilize recursion, mathematical operations, and user input to achieve their objectives.

Uploaded by

antarikhyag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Dudeney Number Checker in Java

The document contains multiple Java programs that demonstrate various mathematical concepts and number classifications, including converting hexadecimal to decimal, identifying evil numbers, checking for pronic numbers, palindromes, disarium numbers, Dudeney numbers, calculating LCM and HCF, and determining perfect and Armstrong numbers. Each program is structured with a main method that executes the respective functionality and prints the results. The implementations utilize recursion, mathematical operations, and user input to achieve their objectives.

Uploaded by

antarikhyag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

HEX TO DEC
import [Link].*;
class DeciHex {
String Hex = "";
void hex(int deci) {
if (deci != 0) {
int k = 0;
char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F'};
k = deci % 16;
Hex = hex[k] + Hex;
deci /= 16;
hex(deci);
}
}
public static void main(String[] args) {
DeciHex ob = new DeciHex();
[Link](4557);
[Link]([Link]);
}
}
2. Evil Number using recursion
import [Link].*;
class Evil {
int bin=0;
int binary(int deci) {
if (deci==0)
return 0;
else{
bin=(deci%2)+10;
return bin* binary(deci/2);
}
}
public static void main(String[] args) {
Evil ob=new Evil();
int c=0;
int k=[Link](17);
while(k>0){
if(k%10==1)
c=c+1;
k/=10;
}
if (c%2==0)
[Link]("ITS AN EVIL NUMBER");
else
[Link]("NOT AN EVIL NUMBER");
}
}
[Link] NO.
import [Link];
public class Pronic {
public static boolean isPronic(int n) {
int sqrt = (int) [Link](n);
return (n == sqrt * (sqrt + 1)) || (n == (sqrt - 1) * sqrt);
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
if (isPronic(number)) {
[Link](number + " is a pronic number.");
} else {
[Link](number + " is not a pronic number.");
} }}
[Link]
import [Link].*;
class palin
{
static int rev(int n, int temp)
{
if (n == 0)
return temp;
temp = (temp * 10) + (n % 10);
return rev(n / 10, temp);
}
public static void main (String[] args)
{
int n = 121;
int temp = rev(n, 0);
if (temp == n)
[Link]("yes");
else
[Link]("no" );
}
}
[Link]
import [Link];
class Disarium{
int num; int size;
public Disarium(int nn){
num = nn; size = 0;
}
public void countDigit(){
size = [Link](num).length();
}
public int sumofDigits(int n, int p){
if(n < 10)
return n;
int term = (int)[Link](n % 10, p);
return term + sumofDigits(n / 10, p - 1);
}
public void check(){
if(num == sumofDigits(num, size))
[Link]("Disarium number!");
else
[Link]("Not a disarium number.");
}
public static void main(String[] args){
Scanner in = new Scanner([Link]);
[Link]("Enter the number: ");
int n = [Link]([Link]());
Disarium obj = new Disarium(n);
[Link]();
[Link]();
}
6. Dudeney no.
import [Link];
public class NumDude
{
int num;
public NumDude()
{
num=0;
}
public void input()
{
Scanner in = new Scanner([Link]); [Link]("Enter the number:
");
num = [Link]();
}
public int sumDigits (int x)
{
if (x==0)
return 0;
else
return x%10 +sumDigits (x/10);
}
public void isDude()
{
int sdig=sumDigits (num);
if((sdig*sdig*sdig)== num)
[Link]("Dudency number ");
else
[Link]("Not a Dudency number ");
}
public static void main(String args[]) {
NumDude ob=new NumDude();
[Link]();
[Link]();
}}
[Link]
class Main {
static int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
public
static void main(String[] args) {
int a = 15, b = 20;
[Link]("LCM of " + a + " and " + b + " is " + lcm(a, b));
}
}
[Link]
public class Main
{
static int gcd(int num1, int num2)
{
if (num1 == 0)
return num2;
if (num2 == 0)
return num1;
if (num1 == num2)
return num1;
if (num1 > num2)
return gcd(num1-num2, num2);
return gcd(num1, num2-num1);
}
public static void main(String[] args)
{
int num1 = 98, num2 = 56;
[Link]("GCD of " + num1 +" and " + num2 + " is " +
gcd(num1, num2));
}
}
[Link] NUMBER
import [Link];
class Perfect{
int num;
public Perfect(int nn){
num = nn;
}
public int sum_of_factors(int i){
if(i > num / 2)
return 0;
if(num % i == 0)
return i + sum_of_factors(i + 1);
return sum_of_factors(i + 1);
}
public void check(){
if(num == sum_of_factors(1))
[Link]("Perfect number!");
else
[Link]("Not a perfect number.");
}
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
[Link]("Enter the number: ");
int n =[Link]();
Perfect obj = new Perfect(n);
[Link]();
}
}
[Link]
public class Main
{
public static void main (String[]args)
{
int num = 1634, len;
len = order (num);
if (armstrong (num, len))
[Link](num + " is armstrong");
else
[Link](num + " is armstrong");
}
static int order (int x)
{
int len = 0;
while (x != 0 )
{
len++;
x = x / 10;
}
return len;
}
static boolean armstrong (int num, int len)
{
int sum = 0, temp, digit;
temp = num;
while (temp != 0)
{
digit = temp % 10;
sum = sum + (int)[Link](digit, len);
temp /= 10;
};
return num == sum;
}
}

Common questions

Powered by AI

The Java programs calculate the LCM using the formula LCM(a, b) = (a / GCD(a, b)) * b, where GCD is the greatest common divisor. Both methods rely on finding the GCD via the Euclidean algorithm recursively. The LCM is then found by multiplying one number by the quotient of the other number and the GCD. This approach efficiently computes LCM by leveraging the mathematical relationship between GCD and LCM, though different code variations may use iterative or recursive means for GCD computation .

The program identifies a 'Disarium' number by summing the digits raised to the power of their respective positions. A method countDigit first determines the number of digits. Then, through sumofDigits, the program calculates the sum of each digit powered by its positional index. If the total equals the original number, it is considered a Disarium number, illustrating the mathematical principle of digit power summations .

A 'Dudeney' number is determined when the cube of the sum of its digits equals the original number itself. The program prompts user input to define the number and then recursively sums the digits. If the cube of this sum matches the input number, it is identified as a Dudeney number, reflecting a unique relation between a number's digit sum and its numerical value .

The program verifies an 'Armstrong number' by raising each digit of the number to a power equal to the total number of digits (order) and summing these powered digits. It uses a while loop to extract digits, compute their power using Math.pow, and sum these values. If the sum equals the original number, it is confirmed as an Armstrong number. Such numbers are crucial in error checking algorithms and cryptography due to their unique properties of self-descriptive numbers .

The recursive method in the DeciHex class converts a decimal number to hexadecimal by repeatedly dividing the number by 16 and storing the remainder. Each remainder corresponds to a hexadecimal digit, selected from the array {'0', '1', '2', ..., 'F'}. This recursive process continues until the input number becomes zero. The remainder digits are concatenated in reverse order to form the complete hexadecimal string representation of the original decimal number .

The program checks if a number is a palindrome by reversing the digits through recursive method rev. It constructs the reversed number by recursively removing the last digit and adding it to the reverse multiply by tens until the original number is reduced to zero. Finally, it compares the reversed number with the original to determine if the number is a palindrome (i.e., it reads the same forwards and backwards).

The program finds the HCF using the recursive Euclidean algorithm, where it repeatedly replaces the larger number by the remainder of the division of the larger number by the smaller number until one of the numbers becomes zero. The non-zero number is then the GCD, which is the HCF. This method efficiently computes the greatest common divisor by exploiting the property that GCD of two numbers also divides their difference, maintaining computational simplicity and speed .

In the program, the number 17 is considered an 'Evil Number' because its binary representation contains an even number of 1's. The recursive function converts 17 into a binary form by using base 2 and multiplies partial results by 10 to create a full binary string representation. After obtaining binary 10001, the program counts the 1's; since there are an even number, it classifies 17 as 'Evil' .

A 'Pronic Number' is determined by checking if it can be stated as the product of two consecutive integers, n = x * (x + 1). The program calculates the square root of the number to find potential consecutive integers and checks the pronic condition by evaluating both x(x + 1) and (x - 1)x as potential forms. Pronic numbers are significant because they represent a sequence of integers that are explicitly products of consecutive integers, which have mathematical interest and use in combinatorial designs .

The program checks for a 'Perfect Number' by calculating the sum of its divisors (excluding itself), using a recursive function to accumulate the divisors that evenly divide the number. If this sum equals the original number, it is a perfect number. Perfect numbers are important in number theory; they are closely related to Mersenne primes and have properties that can contribute to encryption algorithms and other computational theories .

You might also like