×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Java Examples : Patterns Part 3 Java Method Overloading
Numbers Program In Java - Java
21385    Arnab De    14/07/2017

Mathemetical Programs

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

  • Prime
  • Automorphic
  • Perfect
  • Smith
  • Special
softetechnologies

What is An Armstrong number?

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.

Write a program to check a number is Armstrong number or not.

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");
}
}
}

Write a program to find all Armstrong number in the range of 0 and 999.

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+"");
}
}
}
}

What is prime number?

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 

  • the number can not divisible by any number between 2 to n-1
  • the number can not divisible by any number between 2 to n/2
  • the number can not divisible by any number between 2 to √n
softetechnologies

Few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, and 23.

Write a program to check a number is Prime number or not.


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");
}
}
}

Print all the prime no between 10 to 100.

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 + "");
}

}
}
}

Print all the tweensprime no between 3 to 100.

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));
}
}
}
}

Print The Prime Factor Of A Numbers.

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+"");
}
}

What is automorphic number?

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.

Write a program to check a number is automorphic number or not.

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");
}
}
}

softetechnologies

Print All automorphic number from 3 to 1000

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 + "");
}
}
}
}


What is Perfect number?

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.

Check a no is Perfect number or not.

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");
}
}
}

Print All perfect number from 3 to 1000

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");
}
}
}
}

softetechnologies

What is Smith 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.

Write a Program to check a number is smith or not.

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");
}
}
}

A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14 and Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits= 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, output the message “Special 2-digit number” otherwise, output the message “Not a Special 2-digit number”.[ICSE 2014]

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");
}
}
}

Java Examples : Patterns Part 3 Java Method Overloading
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Albert Einstein
I have no special talent. I am only passionately curious.
Albert Einstein
760
49.97
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     41001
01/01/2018     35638
28/06/2017     33618
25/06/2018     32592
02/08/2017     31869
06/07/2017     26486
01/08/2017     26404
15/05/2017     26074
14/07/2017     21385
21/04/2018     20395
softetechnologies