0% found this document useful (0 votes)
160 views11 pages

Programs On Number Based Java

The document describes 23 different types of number programs including programs to calculate factorials, Fibonacci numbers, prime numbers, palindromes, perfect numbers, and other mathematical properties and relationships between numbers. The programs are written in Java code and utilize basic programming constructs like loops, conditionals, and methods to analyze numbers and determine their properties.

Uploaded by

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

Programs On Number Based Java

The document describes 23 different types of number programs including programs to calculate factorials, Fibonacci numbers, prime numbers, palindromes, perfect numbers, and other mathematical properties and relationships between numbers. The programs are written in Java code and utilize basic programming constructs like loops, conditionals, and methods to analyze numbers and determine their properties.

Uploaded by

Dwijesh Donthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

NUMBER BASED PROGRAMS

1. FACTORIAL
5!=1*2*3*4*5=120
class b
{
public b(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;

System.out.println("Factorial="+fact);

}}

2. FIBONNACCI
Eg., 0 ,1,2,3,5,8(sum of the adjacent numbers)

class b
{
public b(int n)
{
int a=0;
int b=1;
for(int i=2;i<=n;i++)
{
int c=a+b;
System.out.println("a="+a+" b="+b+" c="+c);

a=b;
b=c;
}

}}
3. PALINDROME
E.g.: 34343
- Reverse of the number should be equal to the original number
class b
{
public b(int n)
{
int y=n,s=0,r=0;
while(n>0)
{
int d=n%10;
r=r*10+d;
n=n/10;
}
if(r==y)
Page 1 of 11
System.out.println("Palindrome number");
else
System.out.println("Not palindrome number");
}}

4. PRIME
Number divisible by one and itself
class b
{
public b(int n)
{
int y=n,s=0,c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;

}
if(c==2)
System.out.println("Prime number");
else
System.out.println("Not prime number");
}}
5. PERFECT

E.g.: 6=1+2+3(sum of the factors)

class b
{
public b(int n)
{
int y=n,s=0;
for(int i=1;i<n;i++)
{
if(n%i==0)
s=s+i;

if(s==n)
System.out.println("Special number");
else
System.out.println("Not Special number");
}}
6. ARMSTRONG
// Armstrong 153=13+53+33
class arm
Page 2 of 11
{
public arm(int n)
{
int y=n,s=0;
while(n>0)
{
int d=n%10;
s=s+d*d*d;
n=n/10;
}
if(s==y)
System.out.println("Armstrong number");
else
System.out.println("Not Armstrong number");
}}
7. AUTOMORPHIC
// 25 = 625, 5=25
class b
{
public b(int n)
{
int y=n,s=0,c=0;
while(n>0)
{
int d=n%10;
c++;
n=n/10;
}
int p=y*y,k=0;
double r=0;
int ld=p % Math.pow(10,c); // to extract the last digits from square as per
//number of digits

if(ld==y)
System.out.println("Automorphic number");
else
System.out.println("Not Automorphic number");
}}

8. Narcissistic number

E.g.: 1634 = 14+64+34+44


class b
{
public b(int n)
{
int y=n, m=n, c=0; double s=0;
while(n>0)
{
int d=n%10;
Page 3 of 11
c++;
n=n/10;
}
while(y>0)
{
int d=y%10;
s=s+Math.pow(d,c); // power remains constant (no. of digits)
y=y/10;
}
if(s==m)
System.out.println("narcissistic number ");
else
System.out.println("Not narcissistic number ");
}}

9. Disarium number
E.g.: - 135 = 11+32+53
class b
{
public b(int n)
{
int y=n, m=n, c=0; double s=0;
while(n>0)
{
int d=n%10;
c++;
n=n/10;
}
while(y>0)
{
int d=y%10;
s=s+Math.pow(d,c); // power decreases
c--;
y=y/10;
}
if(s==m)
System.out.println("Disarium number");
else
System.out.println("Not Disarium number");
}}

10. SPECIAL
145=1!+4!+5!
class b
{
public b(int n)
{
int y=n,s=0,c=0;
Page 4 of 11
while(n>0)
{
int d=n%10;
int fact=1;
for(int i=1;i<=d;i++)
fact=fact*i;
s=s+fact;
n=n/10;
}

if(s==y)
System.out.println("Special number");
else
System.out.println("Not Special number");
}}
11. MAGIC
// sum of digits is equal to 1 eg.,91=9+1=10=1+0=1
class magic
{
public magic(int n)
{
int s=n;
int d;
while(s>9)
{
n=s;s=0;
while(n>0)
{
d=n%10;
n=n/10;
s=s+d;

}}
if(s==1)
System.out.println("Magic");
else
System.out.println("not Magic");
}}
12. TWIN PRIME
E.g.: 5 , 3
import java.io.*;
class twin
{
public int win(int n)
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
Page 5 of 11
c++;
}
return c;

public void main()throws IOException


{
int x,y;
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter a number");
int p=Integer.parseInt(in.readLine());
System.out.println("Enter second number");
int q=Integer.parseInt(in.readLine());

x=win(p);
y=win(q);
if((x==2 && y==2)&& (p-q==2 || q-p==2))
System.out.println(" Twin Prime");
else
System.out.println("not Twin Prime");
}}

13. Twisted prime


E.g.: 13

class twisted
{
public void twist(int n)
{
int c=0,m=n,r=0,d;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
} // to prime check original number
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}// reverse of the number

int cr=0;
for(int i=1;i<=r;i++)
{
if(r%i==0)
Page 6 of 11
cr++;
} // to prime check reverse number

if(cr==2 && c==2 )


System.out.println(" Twisted Prime ");
else
System.out.println("not Twisted Prime”);

}
}

14. PALPRIME
Eg: 151
class primepal
{
public void win(int n)
{
int c=0,m=n,r=0,d;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}

if(m==r && c==2)


System.out.println("Prime palindrome");
else
System.out.println("not Prime palindrome");

}
}

15. AMICABLE
Eg: 220,284

import java.io.*;
class amicable
{
public int a(int p)
{
int s=0;
for(int i=1;i<p;i++)
{
if(p%i==0)
Page 7 of 11
s=s+i;
}
return s;
}

public void main()throws IOException


{
int x,y;
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter a number");
int m=Integer.parseInt(in.readLine());
System.out.println("Enter second number");
int n=Integer.parseInt(in.readLine());
x=a(m);
y=a(n);
if(x==n && y==m)
System.out.println("Amicable number");
else
System.out.println("not Amicable number");
}}

16. Unique number


Eg: 1275

class uni
{
public uni(int n)
{
int y=n, m=n,c=0; double s=0;
for(int i=0;i<=9;i++)
{

while(n>0)
{
int d=n%10;
n=n/10;
if(d==i)
c++;
}
n=y;
}

if(c<=1)
System.out.println("Unique number");
else
System.out.println("not Unique number");

}}
Page 8 of 11
17. Duck number
Eg: 1275

class duck
{
public duck(String x)
{
if(x.charAt(0)=='0') // to test the first digit is not equal to zero
System.out.println("Not a Duck number");
else
{
int n=Integer.parseInt(x);
int c=0; //counter for zero's
while(n>0)
{
int d=n%10;

if(d==0)
c++;
n=n/10;
}
if(c>=1)
System.out.println("Not Duck number");
else
System.out.println("Duck number");

}
}}

18. BINARY TO DECIMAL

e.g., 1101 =1*23+1*22+0*21+1*20 =13


class b
{
public b(int n)
{
double s=0;
int k=0;
while(n>0)
{
int d=n%10;
s=s+d*Math.pow(2,k);
k++;
n=n/10;
}
System.out.println("Binary to decimal="+(int)s);
}}

Page 9 of 11
19. DECIMAL TO BINARY

//12 = 1100
class b
{
public b(int n)
{
String s="";
int k=0;
while(n>0)
{
int d=n%2;
s=d+s;
n=n/2;
}
System.out.println("Decimal to Binary="+s);
}}

20. GCD
class aa
{
public aa(int a,int b)
{
int p=a*b,hcf=0;
for(int i=1;i<=p;i++)
{
if(a%i==0 && b%i==0)
hcf=i;
}
System.out.println("Hcf"+hcf);
System.out.println("LCM"+(p/hcf));
}}

21. GCD – Long Division Method


class gcd
{
public gcd(int a,int b)
{
int p=a*b,hcf=0,r=0;

int n=Math.max(a,b);
int d=Math.min(a,b);

do
{
r=n%d;
if(r==0)
{
hcf=d;
Page 10 of 11
break;
}
else
{
n=d;
d=r;
}
}while(r!=0);
System.out.println("Hcf"+hcf);
System.out.println("LCM"+(p/hcf));
}}

22. BUZZ
class aa
{
public aa(int a)
{
if(a%7==0 || a%10==7)
System.out.println("Buzz number "+a);
else
System.out.println("Not Buzz number "+a);
}}

23. Neon number


Eg: 9 – (81 -> 8+1=9)
class b
{
public b(int n)
{
int p=n*n;// Square of the number
int y=n,s=0;
while(p>0)
{
int d=p%10;
s=s+d;
p=p/10;
}
if(s==n)
System.out.println("Neon number");
else
System.out.println("Not Neon number");
}}

Page 11 of 11

You might also like