Some mathematical numbers are identified by different names. These names are given by different mathematicians. We can establish those numbers, logically by the Java program. For that we first know the mathematical definition of the numbers after then we must think how we can represent programmatically.
Some example are
If the sum of the cubes of digits of a number is qual to the main number then that numbers known as Armstrong number. For example, 153 is an Armstrong number since 13 + 53 + 33 = 153.
import java.util.*;
public class Amstrong
{
public static void main(String []args)
{
int n,s=0,r;
Scanner sc=new Scanner(System.in);
System.out.print("Enter A no : ");
n=sc.nextInt();
int temp=n;
while(n>0)
{
r=n%10;
s+=Math.pow(r,3);
n /=10;
}
if(s==temp)
{
System.out.print(temp + " is a Amstrong Number");
}
else
{
System.out.print(temp + " is not a Amstrong Number");
}
}
}
public class AmstrongAll
{
public static void main(String []args)
{
int n,s=0,r,t;
for(t=100;t<1000;t++)
{
s=0;
n=t;
while(n>0)
{
r=n%10;
s+=Math.pow(r,3);
n /= 10;
}
if(s==t)
{
System.out.println(s+"");
}
}
}
}
If a whole number greater than 1, is divisible by the 1 and itself. then it is known as Prime Number.
Three others view of the logic
Few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, and 23.
import java.util.*;
public class Prime
{
public static void main(String []args)
{
int n,d=2;
boolean fl=true;
Scanner sc=new Scanner(System.in);
System.out.print("Enter A no : ");
n=sc.nextInt();
while(d<=Math.sqrt(n))
{
if(n%d==0)
{
fl=false;
break;
}
d++;
}
if(fl)
{
System.out.println(n + " is a Prime Number");
}
else
{
System.out.println(n + " is not a Prime Number");
}
}
}
import java.util.*;
public class PrimeRange
{
public static void main(String []args)
{
int n,d;
boolean fl;
Scanner sc=new Scanner(System.in);
for(n=10;n<=100;n++)
{
fl=true;
d=2;
while(d<=Math.sqrt(n))
{
if(n%d==0)
{
fl=false;
break;
}
d++;
}
if(fl)
{
System.out.println(n + "");
}
}
}
}
public class TweensPrime
{
public boolean IsPrime(int n)
{
int d=2;
boolean fl=true;
while(d<=Math.sqrt(n))
{
if(n%d==0)
{
fl=false;
break;
}
d++;
}
return fl;
}
public static void main(String []args)
{
int n;
TweensPrime tp=new TweensPrime();
for(n=2;n<=98;n++)
{
if(tp.IsPrime(n)&& tp.IsPrime(n+2))
{
System.out.println(n + " , " + (n+2));
}
}
}
}
import java.util.*;
public class PrimeFactors
{
public boolean IsPrime(int n)
{
int d=2;
boolean fl=true;
while(d<=Math.sqrt(n))
{
if(n%d==0)
{
fl=false;
break;
}
d++;
}
return fl;
}
public static void main(String []args)
{
int n,i,no,d=2;
Scanner sc=new Scanner(System.in);
System.out.print("Enter A no : ");
no=sc.nextInt();
n=no;
PrimeFactors pf=new PrimeFactors();
while(!pf.IsPrime(n))
{
if(n%d==0)
{
if(pf.IsPrime(d))
{
System.out.println(d+"");
n=n/d;
d=2;
continue;
}
}
d++;
}
System.out.println(n+"");
}
}
If a number's square ends with that number itself, then this number called automorphic number. It is also known as the circular number.
For example, 52 = 25, 62 = 36, 252 = 125, 3762 = 141376. So 5, 6, 25 and 376 are all automorphic numbers.
import java.util.*;
public class Automorphic
{
public static void main(String []args)
{
int n,p=0,d=10;
Scanner sc=new Scanner(System.in);
System.out.print("Enter A no : ");
n=sc.nextInt();
int k=n*n;
while(p<n)
{
p=k%d;
d *= 10;
}
if(p==n)
{
System.out.println(n + " is a Automorphic Number");
}
else
{
System.out.println(n + " is not a Automorphic Number");
}
}
}
public class AutomorphicRange
{
public static void main(String []args)
{
int k,n,p=0,d=10;
boolean fl=true;
for(n=3;n<=1000;n++)
{
k=n*n;
p=0;
d=10;
while(p<n)
{
p=k%d;
d *= 10;
}
if(p==n)
{
System.out.println(n + "");
}
}
}
}
If sum of the proper divisors is equal to the number itself, then that positive number known as perfect number. The lowest perfect number is 6, it's proper divisors is 1, 2, and 3. again sum of the divisors are 6. Examples of some Other perfect numbers are 28 and 496.
import java.util.*;
public class PerfectNo
{
public static void main(String []args)
{
int n,d=2,s=1;
Scanner sc=new Scanner(System.in);
System.out.print("Enter A no : ");
n=sc.nextInt();
while(d<=n/2)
{
if(n%d==0)
{
s += d;
}
d++;
}
if(s==n)
{
System.out.println(n + " is a Perfect Number");
}
else
{
System.out.println(n + " is not a Perfect Number");
}
}
}
public class PerfectNoRange
{
public static void main(String []args)
{
int n,d,s;
for(n=5;n<=1000;n++)
{
d=2;
s=1;
while(d<=n/2)
{
if(n%d==0)
{
s += d;
}
d++;
}
if(s==n)
{
System.out.println(n + " is a Perfect Number");
}
}
}
}
If the sum of the digits of a number is equal to the sum of the prime factors of the numbers and product of the prime factor is also same as the number itself then that number known as Smith Number.
Example, 378 = 2 * 3 * 3 * 3 * 7 is a Smith number since 3 + 7 + 8 = 2 + 3 + 3 + 3 + 7.
import java.util.*;
public class SmithNo
{
public boolean IsPrime(int n)
{
int d=2;
boolean fl=true;
while(d<=Math.sqrt(n))
{
if(n%d==0)
{
fl=false;
break;
}
d++;
}
return fl;
}
public static void main(String []args)
{
int ds=0,n,d=2,s=0,m=1,no;
Scanner sc=new Scanner(System.in);
System.out.print("Enter A no : ");
n=sc.nextInt();
no=n;
SmithNo sn=new SmithNo();
while(!sn.IsPrime(n))
{
if(n%d==0)
{
if(sn.IsPrime(d))
{
s += d;
m *= d;
n=n/d;
d=2;
continue;
}
}
d++;
}
s += n; //sum of prime factor
m *= n; //product of prime factor
if(m==no)
{
int temp=no;
while(temp>0)
{
ds += temp%10;
temp /=10;
}
if(ds==s)
{
System.out.println(n + " is a msith no");
}
else
{
System.out.println(n + " is not a smith no 2");
}
}
else
{
System.out.println(n + " is not a smith no 1");
}
}
}
import java.util.*;
public class SpecialNo
{
public static void main(String []args)
{
int ds=0,n,d=2,s=0,m=1,no;
Scanner sc=new Scanner(System.in);
System.out.print("Enter A no : ");
n=sc.nextInt();
no=n;
while(no>0)
{
s += no%10;
m *= no%10;
no /=10;
}
if(s+m==n)
{
System.out.println(n + " is a Special 2-digit number");
}
else
{
System.out.println(n + " is not a Special 2-digit number");
}
}
}