[Go to site: main page, start]

0% found this document useful (0 votes)
10 views12 pages

Java Programs for Number Checks

The document provides a series of Java programming exercises focusing on various number classifications, including Lucky numbers, Armstrong numbers, Duck numbers, Amicable numbers, and more. Each section includes a brief definition of the number type, example inputs, and corresponding Java code to check if a number belongs to that category. The document serves as a guide for implementing these checks in Java programming.

Uploaded by

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

Java Programs for Number Checks

The document provides a series of Java programming exercises focusing on various number classifications, including Lucky numbers, Armstrong numbers, Duck numbers, Amicable numbers, and more. Each section includes a brief definition of the number type, example inputs, and corresponding Java code to check if a number belongs to that category. The document serves as a guide for implementing these checks in Java programming.

Uploaded by

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

“”DEBASHIS SIR”” Page |1

Check Lucky Number Scanner scanner = new


Write a Java program to check whether a Scanner([Link]);
number is a Luck number or not. [Link]("Input an integer: ");
Lucky numbers are defined via a sieve as String input = [Link]();
follows. int number =
Begin with a list of integers starting with 1 : [Link](input);
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, [Link]("Is Lucky
17, 18, 19, 20, 21, 22, 23, 24, 25, . . . . number? "+isLucky(number));
Now eliminate every second number : }
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, ... }
The second remaining number is 3, so remove Check Armstrong Number
every 3rd number: Write a Java program to check whether a
1, 3, 7, 9, 13, 15, 19, 21, 25, ... number is an Armstrong Number or not.
The next remaining number is 7, so remove Armstrong (Michael F. Armstrong) number is a
every 7th number: number that is equal to the sum of cubes of its
1, 3, 7, 9, 13, 15, 21, 25, ... digits. For example 0, 1, 153, 370, 371 and 407
Next, remove every 9th number and so on. are the Armstrong numbers
Finally, the resulting sequence is the lucky import [Link].*;
numbers. public class solution {
import [Link].*; public static boolean is_Amstrong(int n) {
public class solution { int remainder, sum = 0, temp = 0;
public static boolean isLucky(int n) { temp = n;
//Create an array of size 10 to initialize all while (n > 0) {
elements as remainder = n % 10;
//false to check if a digit is sum = sum + (remainder * remainder *
already seen or not. remainder);
boolean temp[]=new boolean[10]; n = n / 10;
for (int i = 0; i < 10; i++) }
temp[i] = false; return sum == temp;
while (n > 0) }
{
// Find the last digit public static void main(String[] args) {
int digit = n % 10; Scanner scanner = new
// Return false if digit is already seen, Scanner([Link]);
if (temp[digit]) [Link]("Input an integer: ");
return false; String input = [Link]();
temp[digit] = true; int number =
n = n / 10; [Link](input);
} [Link]("Is Armstrong
return true; number? "+is_Amstrong(number));
} }
}
public static void main(String[] args) { Check Duck Number
“”DEBASHIS SIR”” Page |2

Write a Java program to check whether a of each is equal to the other number.
number is a Duck Number or not. The first ten amicable pairs are: (220, 284),
Note: A Duck number is a number which has (1184, 1210), (2620, 2924), (5020, 5564),
zeroes present in it, but there should be no (6232, 6368), (10744, 10856), (12285, 14595),
zero present in the beginning of the number. (17296, 18416), (63020, 76084), and (66928,
For example 3210, 7056, 8430709 are all duck 66992).
numbers whereas 08237, 04309 are not. Test Data
Test Data Input the first number: 220
Input a number : 3210 Input the second number: 284
import [Link]; import [Link];

public class Example15 { public class Example16


{
public static void main(String args[]) public static void main(String args[]) {
{ Scanner in = new Scanner([Link]);
Scanner sc = new Scanner([Link]); [Link]("Input the first
[Link]("Input a number : "); number: ");
String nstr = [Link](); int num1 = [Link]();
[Link]("Input the second
int l = [Link](); number: ");
int ctr = 0; int num2 = [Link]();
char chr; int sum_num1 = 0, sum_num2 = 0;
for (int i = 1; i <= num1; i++) {
for(int i=1;i<l;i++)
if (num1 % i == 0)
{
sum_num1 += i;
chr = [Link](i);
}
if(chr=='0')
for (int i = 1; i <= num2; i++) {
ctr++;
if (num2 % i == 0)
}
sum_num2 += i;
char f = [Link](0); }
if (sum_num1 == sum_num2)
if(ctr>0 && f!='0') [Link]("These numbers
[Link]("Duck number"); are amicable.");
else else
[Link]("Not a duck [Link]("These numbers
number"); are not amicable.");
} [Link]("\
} n");
Check Amicable Numbers }
Write a Java program to check two numbers }
are Amicable numbers or not. Ramanujan Numbers
Amicable numbers are two different numbers Write a Java program to find any number
so related that the sum of the proper divisors between 1 and n that can be expressed as the
“”DEBASHIS SIR”” Page |3

sum of two cubes in two (or more) different if (c3 + d3 > a3 + b3) break;
ways.
//[Link] if (c3 + d3 == a3 + b3) {
13flow/[Link] [Link]((a3+b3) + " =
Here are some examples of Ramanujan ");
numbers : [Link](a + "^3 + " + b
1729 = 1^3 + 12^3 = 9^3 + 10^3 + "^3 = ");
* 10000 [Link](c + "^3 + " + d
1729 = 1^3 + 12^3 = 9^3 + 10^3 + "^3");
4104 = 2^3 + 16^3 = 9^3 + 15^3 [Link]();
* 100000 }
1729 = 1^3 + 12^3 = 9^3 + 10^3 }
4104 = 2^3 + 16^3 = 9^3 + 15^3 }
13832 = 2^3 + 24^3 = 18^3 + 20^3 }
39312 = 2^3 + 34^3 = 15^3 + 33^3 }
46683 = 3^3 + 36^3 = 27^3 + 30^3 }
32832 = 4^3 + 32^3 = 18^3 + 30^3 }
40033 = 9^3 + 34^3 = 16^3 + 33^3 Check Palindrome Number
20683 = 10^3 + 27^3 = 19^3 + 24^3 Write a Java program to check if a number is a
65728 = 12^3 + 40^3 = 31^3 + 33^3 palindrome or not.
64232 = 17^3 + 39^3 = 26^3 + 36^3 In number system a palindromic number is a
import [Link]; number that is the same when written
public class Example21 { forwards or backwards, i.e., of the form.
The first few palindromic numbers are
public static void main(String[] args) { therefore are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22,
33, 44, 55, 66, 77, 88, 99, 101, 111, …
int n = 100000; Test Data
// for each a, b, c, d, check whether a^3 + Input a number: 5
b^3 = c^3 + d^3 import [Link];
for (int a = 1; a <= n; a++) { public class Example24 {
int a3 = a*a*a;
if (a3 > n) break; public static void main(String args[])
{
for (int b = a; b <= n; b++) { Scanner in = new Scanner([Link]);
int b3 = b*b*b; [Link]("Input a number: ");
if (a3 + b3 > n) break; int n = [Link]();
int sum = 0, r;
for (int c = a + 1; c <= n; c++) { int temp = n;
int c3 = c*c*c; while(n>0)
if (c3 > a3 + b3) break; {
r = n % 10;
for (int d = c; d <= n; d++) { sum = (sum*10)+r;
int d3 = d*d*d; n = n/10;
“”DEBASHIS SIR”” Page |4

} i=d; sum=0;
if(temp==sum) while(sum<n)
[Link]("It is a Palindrome {
number."); sum = 0;
else for(int j=1; j<=d; j++)
[Link]("Not a palindrome"); {
} sum=sum+arr[i-j];
} }
Check Keith Number arr[i]=sum;
Write a Java program to check whether a i++;
number is a Keith Number or not. }
In recreational mathematics, a Keith number
or repfigit number (short for repetitive if(sum==n)
Fibonacci-like digit) is a number in the [Link]("Keith Number");
following integer sequence: else
14, 19, 28, 47, 61, 75, 197, 742, 1104, 1537, [Link]("Not a Keith Number");
2208, 2580, 3684, 4788, 7385, 7647, 7909, }
31331, 34285, 34348, 55604, 62662, 86935, }
93993, 120284, 129106, 147640, 156146, Check Automorphic Number
174680, 183186, 298320, 355419, 694280, Write a Java program to check whether a
925993, number is an automorphic number or not.
Test Data In mathematics, an automorphic number is a
Input a number: 75 number whose square "ends" in the same
import [Link]; digits as the number itself. For example, 52 =
public class Example26 { 25, 62 = 36, 762 = 5776, and 8906252 =
793212890625, so 5, 6, 76 and 890625 are all
public static void main(String[] args) { automorphic numbers.
Test Data
Scanner sc = new Scanner( [Link] ); Input a number : 76
[Link]("Input a number: "); import [Link];
int n = [Link](); public class Example14 {
int n1 = n;
String s = [Link](n); public static void main(String args[])
int d=[Link](); {
int arr[]=new int[n]; Scanner sc = new Scanner([Link]);
int i, sum; [Link]("Input a number : ");
for(i=d-1; i>=0; i--) int num = [Link]();
{ int sq_num = num*num;
arr[i]=n1 % 10;
String str_num = [Link](num);
n1=n1/10;
String square = [Link](sq_num);
}
if([Link](str_num))
“”DEBASHIS SIR”” Page |5

[Link]("Automorphic [Link]("Not a Pronic


Number."); Number.");
else }
[Link]("Not an }
Automorphic Number."); Check Harshad Number
} Write a Java program to check whether a
} number is a Harshad Number or not.
Check Pronic Number In recreational mathematics, a harshad
Write a Java program to check whether a number in a given number base, is an integer
number is a Pronic or Heteromecic Number or that is divisible by the sum of its digits when
not. written in that base.
A pronic number is a number which is the Example: Number 200 is a Harshad Number
product of two consecutive integers, that is, a because the sum of digits 2 and 0 and 0 is
number of the form n(n + 1). 2(2+0+0) and 200 is divisible by 2. Number 171
The first few pronic numbers are: is a Harshad Number because the sum of digits
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 1 and 7 and 1 is 9(1+7+1) and 171 is divisible
182, 210, 240, 272, 306, 342, 380, 420, 462 … by 9.
etc. Test Data
Test Data Input a number : 353
Input a number : 110 import [Link];
import [Link]; public class Example12 {

public class Example13 { public static void main(String args[])


{
public static void main(String args[]) Scanner sc = new Scanner([Link]);
{
Scanner sc = new Scanner([Link]); [Link]("Input a number : ");
[Link]("Input a number : "); int num = [Link]();
int num = [Link](); int x = num, y, sum = 0;
int ans = 0;
while(x>0)
for(int i=0; i<num; i++) {
{ y = x%10;
if(i*(i+1) == num) sum = sum + y;
{ x = x/10;
ans = 1; }
break;
if(num%sum == 0)
}
[Link](num+" is a Harshad
}
Number.");
if(ans == 1) else
[Link]("Pronic Number."); [Link](num+" is not a
else Harshad Number.");
}
“”DEBASHIS SIR”” Page |6

} [Link]("Not a Disarium
Check Disarium or Unhappy Number Number.");
Write a Java program to check whether a given }
number is a Disarium number or an unhappy }
number. Check Happy or Unhappy Number
A Disarium number is a number defined by the Write a Java program to check whether a given
following process: number is a happy number or unhappy
Sum of its digits powered with their respective number.
position is equal to the original number. Happy number: Starting with any positive
For example 175 is a Disarium number: integer, replace the number by the sum of the
As 11+32+53 = 135 squares of its digits, and repeat the process
Some other DISARIUM are 89, 175, 518 etc. until the number equals 1, or it loops endlessly
A number will be called Disarium if the sum of in a cycle which does not include 1.
its digits powered with their respective An unhappy number is a number that is not
position is equal with the number itself. happy.
Sample Input: 135. The first few unhappy numbers are 2, 3, 4, 5, 6,
Test Data 8, 9, 11, 12, 14, 15, 16, 17, 18, 20.
Input a number : 25 import [Link];
import [Link]; import [Link];
import [Link];
public class Example11 {
public class Example10 {
public static void main(String args[])
{ public static boolean isHappy_number(int
Scanner sc = new Scanner([Link]); num)
[Link]("Input a number : "); {
int num = [Link](); Set<Integer> unique_num = new
int copy = num, d = 0, sum = 0; HashSet<Integer>();
String s = [Link](num);
int len = [Link](); while (unique_num.add(num))
{
while(copy>0) int value = 0;
{ while (num > 0)
d = copy % 10; {
sum = sum + (int)[Link](d,len); value += [Link](num % 10, 2);
len--; num /= 10;
copy = copy / 10; }
} num = value;
}
if(sum == num)
[Link]("Disarium
return num == 1;
Number.");
}
else
“”DEBASHIS SIR”” Page |7

public static void main(String[] args) digit = (int)(num % 10);


{ m += digit*digit;
[Link]("Input a number: "); num /= 10;
int num = new }
Scanner([Link]).nextInt(); num = m;
[Link](isHappy_number(num) }
? "Happy Number" : "Unhappy Number"); return num == 1;
} }
} }
First 10 Happy Numbers First 10 Lucas Numbers
Write a Java program to find and print the first Write a Java program to display the first 10
10 happy numbers. lucus numbers.
Happy number: Starting with any positive The Lucas numbers or series are an integer
integer, replace the number by the sum of the sequence named after the mathematician
squares of its digits, and repeat the process François Édouard Anatole Lucas, who studied
until the number equals 1, or it loops endlessly both that sequence and the closely related
in a cycle which does not include 1. Fibonacci numbers. Lucas numbers and
Example: 19 is a happy number Fibonacci numbers form complementary
12 + 92=82 instances of Lucas sequences.
82 + 22=68 The sequence of Lucas numbers is: 2, 1, 3, 4, 7,
62 + 82=100 11, 18, 29, ….
12 + 02 + 02=1 import [Link];
import [Link]; public class Example7 {
public class Example9 { public static void main(String[] args) {
public static void main(String[] args){
[Link]("First 10 Happy [Link]("\nFirst ten Lucas a
numbers:"); numbers: ");
for(long num = 1,count = 0;count<8;num+ int n = 10;
+){ int n1 = 2, n2 = 1, n3;
if(happy_num(num)){ if (n > 1){
[Link](num); [Link]("\n2\n1");
count++; for(int i = 2; i < n; ++i){
} n3 = n2;
} n2 += n1;
} n1 = n3;
public static boolean happy_num(long num){ [Link](n2);
long m = 0; }
int digit = 0; }
HashSet<Long> cycle = new else if (n == 1)
HashSet<Long>(); [Link]("\n2");
while(num != 1 && [Link](num)){
m = 0; else
while(num > 0){
“”DEBASHIS SIR”” Page |8

[Link]("Input a positive String str1 = [Link](n);


number."); int x = [Link]();
} long sum_num = 0;
}
First 15 Narcissistic Numbers for(char c : [Link]()){
Write a Java program to generate and show sum_num +=
the first 15 narcissistic decimal numbers. [Link]([Link](c, 10), x);
A Narcissistic decimal number is a non- }
negative integer, n that is equal to the sum of return sum_num == n;
the m-th powers of each of the digits in the }
decimal representation of n, where m is the
}
number of digits in the decimal representation
Check Ugly Number
of n.
Write a Java program to check whether a given
 if n is 153 number is ugly.
 then m , (the number of decimal digits) In number system, ugly numbers are positive
is 3 numbers whose only prime factors are 2, 3 or
 we have 13 + 53 + 33 = 1 + 125 + 27 = 153 5. First 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9,
 and so 153 is a narcissistic decimal 10, 12. By convention, 1 is included.
number . Test Data: Input an integer number: 235
import [Link];
Narcissistic numbers in various bases : public class Exercise1 {
The sequence of base 10 narcissistic numbers public static void main(String[] args){
starts: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, …… Scanner in = new Scanner([Link]);
The sequence of base 8 narcissistic numbers [Link]("Input an integer
starts: 0, 1, 2, 3, 4, 5, 6, 7, 24, 64, 134, 205,…. number: ");
// [Link] int n = [Link]();
public class Example6 { if (n <= 0) {
public static void main(String[] args){ [Link]("Input a correct
for(long n = 0, ctr = 0; ctr < 15; n+ number.");
+){ }
if(is_narc_dec_num(n)){ int x = 0;
[Link](n + while (n != 1) {
" "); if (n % 5 == 0) {
ctr++; n /= 5;
} } else if (n % 3 == 0) {
} n /= 3;
[Link](); } else if (n % 2 == 0) {
} n /= 2;
public static boolean is_narc_dec_num(long } else {
n){ [Link]("It is not an ugly
if(n < 0) return false; number.");
x = 1;
“”DEBASHIS SIR”” Page |9

break; ctr++;
} break;
} }
if (x==0) }
[Link]("It is an ugly }
number."); [Link](ctr + " Kaprekar
[Link]("\n"); numbers.");
} }
} private static String[] split_num(String str,
Kaprekar Numbers < 1000 int idx){
Write a Java program to generate and show all String[] ans1 = new String[2];
Kaprekar numbers less than 1000. ans1[0] = [Link](0, idx);
In number theory, a Kaprekar number for a if(ans1[0].equals("")) ans1[0] = "0";
given base is a non-negative integer, the ans1[1] = [Link](idx);
representation of whose square in that base return ans1;
can be split into two parts that add up to the }
original number again. For instance, 45 is a }
Kaprekar number, because 452 = 2025 and 20 + Split Whole and Fractional Parts
25 = 45. Write a Java program to get whole and
The first few Kaprekar numbers in base 10 are: fractional parts from a double value.
1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, import [Link].*;
4879, 4950, 5050, 5292, … public class Example2 {
public class Example4 { public static void main(String[] args) {
public static void main(String[] args){ double value = 12.56;
int ctr = 0; double fractional_part = value % 1;
int base = ([Link] > 0) ? double integral_part = value - fractional_part;
[Link](args[0]) : 10; [Link]("\nOriginal value: "+value);
for(long n = 1; n <= 1000; n++){ [Link]("\nIntegral part:
String sqr_Str = [Link](n * n, "+integral_part);
base); [Link]("\nFractional part:
for(int j = 0; j < sqr_Str.length() / 2 + 1; "+fractional_part);
j++){ [Link]();
String[] parts = split_num(sqr_Str, j); }
long first_Num = }
[Link](parts[0], base); Multiply Without Operators
long sec_Num = Write a Java program to multiply two integers
[Link](parts[1], base); without multiplication, division, bitwise
if(sec_Num == 0) break; operators, and loops.
if(first_Num + sec_Num == n){ import [Link].*;
[Link]([Link](n, public class solution {
base) + public static int multiply_two_nums(int a, int
"\t" + sqr_Str + "\t " + parts[0] b) {
+ " + " + parts[1]);
“”DEBASHIS SIR”” P a g e | 10

/* 0 multiplied with anything gives 0 */ }


if (b == 0) temp = result;
return 0; }

if (b > 0) return result;


return (a + multiply_two_nums(a, b - }
1));
public static void main(String[] args) {
if (b < 0) Scanner scan = new Scanner([Link]);
return -multiply_two_nums(a, -b); [Link]("Input the base: ");
int b = [Link]();
return -1; [Link]("Input the
} exponent: ");
int e = [Link]();
public static void main(String[] args) {
[Link]();
Scanner scan = new Scanner([Link]);
if (b>0 && e>0)
[Link]("Input first integer: ");
[Link]("Power of the
int num1 = [Link]();
number: "+power(b, e));
[Link]("Input second integer: ");
}
int num2 = [Link]();
}
[Link]();
Generate Magic Square
[Link]("Multiply of two
Write a Java program to generate a magic
integers: "+multiply_two_nums(num1, num2));
square of order n (all row, column, and
diagonal sums are equal).
}
From Wikipedia,
}
In recreational mathematics and combinatorial
Power Without Multiplication or Division
design, a magic square is a n x n square grid
Write a Java program to calculate power of a
(where n is the number of cells on each side)
number without using multiplication(*) and
filled with distinct positive integers in the
division(/) operators.
range 1, 2, ..., n2 such that each cell contains a
import [Link].*;
different integer and the sum of the integers in
public class solution {
each row, column and diagonal is equal. The
public static int power(int b, int e)
sum is called the magic constant or magic sum
{
of the magic square. A square grid with n cells
if (e == 0)
on each side is said to have order n.
return 1;
import [Link].*;
int result = b; public class solution {
int temp = b;
int i, j; public static void main(String[] args) {

for (i = 1; i < e; i++) { Scanner scan = new


for (j = 1; j < b; j++) { Scanner([Link]);
result += temp; [Link]("Input a number: ");
int num = [Link]();
“”DEBASHIS SIR”” P a g e | 11

if ((num % 2 == 0) || (num <=0 )) Write a Java program to check if a given


{ number is a Fibonacci number or not.
[Link](0);
[Link]("Input number must import [Link].*;
be odd and >0");
}
class solution {
int[][] magic_square = new int[num][num];
static boolean isPerfectSquare(int x)
int row_num = num-1;
int col_num = num/2; {
magic_square[row_num][col_num] = 1;
int s = (int) [Link](x);
for (int i = 2; i <= num*num; i++) {
if (magic_square[(row_num + 1) % num] return (s*s == x);
[(col_num + 1) % num] == 0) {
}
row_num = (row_num + 1) % num;
col_num = (col_num + 1) % num;
}
else { static boolean isFibonacci(int x)
row_num = (row_num - 1 + num) %
num; {
}
magic_square[row_num][col_num] = i; return isPerfectSquare(5*x*x + 4) ||
}
isPerfectSquare(5*x*x - 4);
// print the square }
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (magic_square[i][j] < 10)
[Link](" "); public static void main(String[] args)
if (magic_square[i][j] < 100)
[Link](" "); {
[Link](magic_square[i][j] +
Scanner scan = new Scanner([Link]);
" ");
} [Link]("Input a number: ");
[Link]();
} int n = [Link]();

} if (n>0)
}
Check Fibonacci Number {
“”DEBASHIS SIR”” P a g e | 12

[Link]("Is Fibonacci
number? "+isFibonacci(n));

}
}

Common questions

Powered by AI

An ugly number is a positive integer whose prime factors are limited to 2, 3, or 5. To implement a check for an ugly number in Java, repeatedly divide the number by 2, 3, and 5 until it reaches 1. If it can be wholly divided down to 1 using only these factors, it is an ugly number; otherwise, it's not. The program carries out this repeated division and checks the condition iteratively.

A Disarium number is one where the sum of its digits powered with their respective positions equals the number itself. In Java, compute a Disarium number by taking each digit's positional power and summing these powers. If the sum equals the original number, it is a Disarium number. This involves looping over digits, applying Math.pow to calculate powers, and summing results to compare with the original number.

A happy number is one that results in 1 when a specific iterative process on its digits is performed: repeatedly replace the number by the sum of the squares of its digits. If the process loops endlessly in a cycle that does not include 1, the number is "unhappy." In Java, the process involves using a set to detect cycles and repeatedly applying the digit squaring and summing process until 1 is reached. The significance lies in its relation to number classification involving cycles.

A Keith number is a number that appears as a term in a linear recurrence relation similar to the Fibonacci sequence, constructed with its own digits. To identify a Keith number in Java, the number's digits are used to start the sequence. You compute the sum of the initial digits iteratively, appending each sum to the sequence, until the sum equals the target number. If it does, the number is a Keith number. The algorithm involves converting the number to a string to get digit lengths, iterating to sum up previous known sums to find a match.

A Harshad number is an integer divisible by the sum of its digits. In Java, to verify if a number is a Harshad number, calculate the sum of its digits and check if the original number is divisible by this sum. The program iteratively extracts each digit, calculates their sum, and performs the divisibility test. If the number divides evenly by the digits sum, it is a Harshad number.

A palindromic number is a number that is the same when written forwards or backwards. In Java, you can determine if a number is palindromic by reversing the digits of the number and checking if the reversed number is equal to the original number. The provided Java program achieves this by taking each digit from the original number, reversing the order, and then comparing the reversed number with the original one to determine if it is a palindrome.

An automorphic number is one whose square ends with the number itself. To check if a number is automorphic in Java, calculate the square of the number and convert both the square and the original number to strings. Compare the tail end of the square's string with the entire string of the original number. If they match, the number is automorphic. This characteristic defines an automorphic number.

To multiply two integers without using the multiplication operator in Java, use recursive addition. The recursive logic involves adding the first number to itself repeatedly, as many times as indicated by the second number, decrementing the count in each recursive call. If the second number is negative, handle it by multiplying the absolute values and negating the result. This method uses addition and controlled recursion instead of directly employing the multiplication operator.

To generate a magic square in Java, use the Siamese method for odd-ordered squares, which places numbers sequentially in specific patterns to achieve equal sums. The approach places numbers diagonally, wrapping around the grid boundaries, and stepping down when encountering filled cells. This method ensures each row, column, and diagonal has the same sum by arranging numbers methodically. Odd and methodical placement ensures that's achieved across the entire grid.

A Kaprekar number is one where its square can be split into two parts that add up to the original number. To verify a number as Kaprekar in Java, square the number, convert the result to a string, and try various splits between digits. If any split yields two parts that sum to the original number, it is a Kaprekar number. The program iteratively goes through possible split points within the digit sequence length.

You might also like