0% found this document useful (0 votes)
18 views7 pages

Number Classification Algorithms in Java

The document contains Java code snippets that define classes to check if a given number meets certain mathematical properties such as being automorphic, ambica, Armstrong, happy, Kaprekar, magic, neon, perfect, special, triangular, etc. Each class contains a main method that takes user input, performs calculations to test the property, and prints whether the number meets the property or not.

Uploaded by

Krishna Gupta
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)
18 views7 pages

Number Classification Algorithms in Java

The document contains Java code snippets that define classes to check if a given number meets certain mathematical properties such as being automorphic, ambica, Armstrong, happy, Kaprekar, magic, neon, perfect, special, triangular, etc. Each class contains a main method that takes user input, performs calculations to test the property, and prints whether the number meets the property or not.

Uploaded by

Krishna Gupta
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

import [Link].

Scanner;
class Automorphic
{
void main()
{
Scanner x = new Scanner ([Link]);
[Link]("Enter a No.");
int n = [Link]();
int N = n;
int c= 0 ;
while(N>0)
{
c++;
N/=10;
}
N = n*n;
int d = (int)[Link](10,c);
int r = N%d;
if(r==n)
{
[Link]("Entered No. is Automorphic No.");
}
else
{
[Link]("Entered No. is not Automorphic No.");
}
}
}

import [Link];
class ambica
{
void main()
{
[Link]("Enter two no.s");
Scanner x = new Scanner([Link]);
[Link]("Enter two no.s");
long n = [Link]();
int s=0,s1=0;
for (long i =1;i<=n/2;i++)
{
if(n%i==0)
{
s+=i;
}
}
long k = [Link]();
for (long i =1;i<=k/2;i++)
{
if(k%i==0)
{
s1+=i;
}
}
if(s==k&&s1==n)
[Link]("the entered number is a ambica number");
else
[Link]("the entered number is not a ambica number");
}
}

import [Link];
class ARMSTRONG
{
void main()
{
int s=0,c=0,k=0;
Scanner x = new Scanner([Link]);
[Link]("ENTER THE NO.");
int n =[Link]();
int a = n;
while(a!=0)
{
c=a%10;
k=c*c*c;
s+=k;
a/=10;
}
if(s==n)
{
[Link]("THE ENTERED NO. "+n+" IS ARMSTRONG");
}
else
{
[Link]("THE ENTERED NO. "+n+" IS NOT
ARMSTRONG");
}
}
}

public class HAPPYNO


{
void digitFrequency()
{
[Link]("Happy Numbers are");
int c = 0;
int s = 0;
for(int i = 0; i<=1000;i++)
{
int a = i ;
while(a>9)
{
while(a!=0)
{
c=a%10;
s+=(c*c);
a/=10;
}
a=s;
s=0;
//[Link](a);
}
if(a==1)
{
[Link](i);
}
}
}
}

import [Link];
class isc2017
{
void main()
{
Scanner x = new Scanner ([Link]);
[Link]("Enter a No. of Boxes To be packed ");
int n = [Link]();
int N = n;
int a[] = {48,24,12,6};
int b[] = {0,0,0,0};
for(int i = 0 ;i<4;i++)
{
b[i] = N/a[i];
N%=a[i];
[Link]("Cartoon of capacity "+a[i]+" boxes
required
:: "+b[i]);
}
[Link]("Remaining No. of Boxes unpacked "+N);
}
}

import [Link];
class kaprekar_No
{
void main()
{
Scanner x = new Scanner ([Link]);
[Link]("Enter a No.");
int n = [Link]();
int N = n;
int c= 0 ;
while(N>0)
{
c++;
N/=10;
}
N = n*n;
int d = (int)[Link](10,c);
int r = N%d;
int l = N/d;
if((l+r)==n)
{
[Link]("Entered No. is a Kaprekar No.");
}
else
{
[Link]("Not kap");
}
}
}

import [Link].*;
class MAGIC
{
void generate()
{
Scanner x = new Scanner([Link]);
[Link]("Enter a number");
int n = [Link]();
if(n%9==1)
{
[Link]("A MAGIC NUMBER");
}
else
{
[Link]("NOT A MAGIC NUMBER");
}
}
}

import [Link];
class NEON
{
void main()
{
int s=0,c=0,k=0;
Scanner x = new Scanner([Link]);
[Link]("ENTER THE NO.");
int n =[Link]();
int a = n*n;
while(a!=0)
{
c=a%10;
s+=c;
a/=10;
}
if(s==n)
{
[Link]("THE ENTERED NO. "+n+" IS A NEON NO. ");
}
else
{
[Link]("THE ENTERED NO. "+n+" IS NOT NEON NO.
");
}
}
}

import [Link].*;
class PERFECT
{
void check()
{
long n;
long s= 0;
Scanner x = new Scanner([Link]);
[Link]("Enter a number");
n = [Link]();
for (long i =1;i<=n/2;i++)
{
if(n%i==0)
{
s+=i;
}
}
if(s==n)
[Link]("the entered number is a Perfect number");
else
[Link]("the entered number is not a Perfect number");
}
}
import [Link];
class special_no
{
void main()
{
Scanner x = new Scanner ([Link]);
[Link]("Enter a No.");
int n = [Link]();
int N = n;
int c = 0 ;
int s = 0;
int r = 1 ;
while(N>0)
{
c=N%10;
for(int i = 1; i<=c;i++)
r*=i;
s+=r;
N/=10;
r=1;
}
if(s==n)
{
[Link]("Entered No. is Special No.");
}
else
{
[Link]("Not a Special no.");
}
}
}

class TriangularNo
{
void main(int n)
{
int c = 0 ;
int f = 0;
for(int i = 1;i<=n;i++)
{
c+=i;
if(c==n)
{
f=-1;
}
}
if(f==-1)
{
[Link]("Entered No. Is Triangular Number");
}
else
{
[Link]("Entered No. Is Not a Triangular Number");
}
}
}

Common questions

Powered by AI

Determining a Happy number involves iteratively calculating the sum of squares of digits, which can become a computational burden if not optimized via loop detection strategies. In contrast, checking a Perfect number primarily involves straightforward divisor summation. Happy numbers pose greater algorithmic complexity due to potentially infinite loops, whereas Perfect numbers are challenging due to the extensive divisor search required .

A Neon number is determined by squaring the number, summing the digits of the square, and comparing this sum with the original number. This reliance on digit summation arises from the need to check if the sum of the individual components of the square satisfies the condition of equality with the original number, emphasizing arithmetic consistency .

The algorithm determines an Automorphic number by checking if the number appears at the end of its square. The variable 'd' is used to extract the last digits of the square equivalent to the number of digits in the original number. It calculates 'd' as 10 raised to the power of the number of digits in the original number, providing a modulus value to extract these digits .

The Armstrong number check involves calculating the sum of the cubes of its digits and verifying if this sum equals the number itself. In contrast, Special numbers are determined by summing the factorials of their digits and checking for equality with the number. Both involve digit-level operations but differ in the computational operations — cubes versus factorials .

The algorithm identifies Magic numbers by checking if its modulo 9 equals 1, leveraging the property that the sum of digits of a multiple of 9 is also a multiple of 9. This divisibility rule is crucial for the check, making it directly linked to arithmetic properties rather than complex computations .

An Ambica number is verified by checking if the sum of proper divisors of one number equals the other number, and vice versa. This mathematical basis is derived from the concept of amicable numbers, where two numbers are said to be amicable if they are perfect dividers of each other without including themselves .

The Happy number algorithm involves repeatedly summing the squares of its digits until the result is either 1 (indicating a Happy number) or loops endlessly (Sad number). This iterative approach is unique because it relies on a feedback loop, contrasting with other methods that work via single-step arithmetic or divisor checks .

The algorithm for a Kaprekar number involves squaring the number, splitting the resultant number into two parts, and checking if their sum equals the original number. It differs from Automorphic number logic, where only the ending digits of the square are compared with the original number; Kaprekar numbers require adding the parts post-split .

The logic for a Special number can be generalized through factorial operations, but this is specific to numbers where the sum of factorials of digits equals the original number. Such logic cannot universally apply as it heavily relies on factorial growth, rendering it limited in scalability concerning number size .

The algorithm identifies a Perfect number by summing its divisors (excluding the number itself) and checking if this sum equals the number. Triangular numbers are identified by calculating cumulative summations (1+2+3+...+n) and checking if this matches the given number. While Perfect numbers rely on divisibility properties, Triangular numbers depend on cumulative series calculations .

You might also like