Definition Programs
Definition Programs
1.//WAP to enter two number and check number is co prime number or not.
import java.util.*;
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,x,y,c=0;
System.out.println("Enter two number");
x=sc.nextInt();
y=sc.nextInt();
for(i=1;i<x;i++)
{
if(x%i==0 && y%i==0)
c++;
}
if(c==1)
System.out.print("co prime number");
else
System.out.print("Not co prime number");
}
}
2//WAP to enter two number and display LCM value
import java.util.*;
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,x,y,lcm=0;
System.out.println("Enter two number");
x=sc.nextInt();
y=sc.nextInt();
for(i=1;i<=x*y;i++)
{
if(i%x==0 && i%y==0)
{
lcm=i;
break;
}
}
System.out.print("LCM value"+lcm);
1
Important Programs: -
}
}
3//WAP to enter two number and display HCF value
import java.util.*;
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,x,y,hcf=0;
System.out.println("Enter two number");
x=sc.nextInt();
y=sc.nextInt();
for(i=1;i<=x;i++)
{
if(x%i==0 && y%i==0)
{
hcf=i;
}
}
System.out.print("HCF value"+hcf);
}
}
2
Important Programs: -
import java.util.*;
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,x,y,s1=0,s2=0;
System.out.println("Enter two number");
x=sc.nextInt();
y=sc.nextInt();
for(i=1;i<x;i++)
{
if(x%i==0)
s1=s1+i;
}
for(i=1;i<y;i++)
{
if(y%i==0)
s2=s2+i;
}
if(s1==y && s2==x)
System.out.print("Amicable pair");
else
System.out.print("Not Amicable pair");
}
}
5//WAP to enter a number and check number is Armstrong number or not.
3
Important Programs: -
153: 13 + 53 + 33 = 1 + 125+ 27 = 153
125: 13 + 23 + 53 = 1 + 8 + 125 = 134 (Not an Armstrong Number)
1634: 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1643
import java.util.*;
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,x,d,s=0,k=0;
System.out.println("Enter two number");
x=sc.nextInt();
for(i=x;i>0;i=i/10)
k++;
for(i=x;i>0;i=i/10)
{
d=i%10;
s=s+(int)Math.pow(d,k);
}
if(s==x)
System.out.print("Armstrong Number");
else
System.out.print("Not Armstrong Number");
}
}
6//WAP to enter a number and check number is disarium number or not.
11+72+53 = 175
4
Important Programs: -
import java.util.*;
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,x,d,r=0,c=1,s=0;
System.out.println("Enter two number");
x=sc.nextInt();
for(i=x;i>0;i=i/10)
{
d=i%10;
r=r*10+d;
}
for(i=r;i>0;i=i/10)
{
d=i%10;
s=s+(int)Math.pow(d,c++);
}
if(s==x)
System.out.print("Disarium number");
else
System.out.print("Not Disarium number");
}
}
7//WAP to enter a number and check number is Kaprekar number or not.
5
Important Programs: -
import java.util.*;
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,x,y,n,c=0,sq;
System.out.println("Enter a number");
n=sc.nextInt();
sq=n*n;
for(i=n;i>0;i=i/10)
{
c++;
}
x=sq/(int)Math.pow(10,c);
y=sq%(int)Math.pow(10,c);
if(x+y==n)
System.out.print("Kaprekar number");
else
System.out.print("Not Kaprekar number");
}
}
8//WAP to enter a number and check number is Automorphic number or not.
import java.util.*;
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,x,n,c=0,sq;
6
Important Programs: -
System.out.println("Enter a number");
n=sc.nextInt();
sq=n*n;
for(i=n;i>0;i=i/10)
{
c++;
}
x=sq%(int)Math.pow(10,c);
if(x==n)
System.out.print("Automorphic number");
else
System.out.print("Not Automorphic number");
}
}
9//WAP to enter a number and check number is Tri-Automorphic number or
not.
import java.util.*;
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,x,n,c=0,sq;
System.out.println("Enter a number");
n=sc.nextInt();
sq=n*n*3;
for(i=n;i>0;i=i/10)
{
c++;
}
x=sq%(int)Math.pow(10,c);
if(x==n)
System.out.print("Tri-Automorphic number");
else
System.out.print("Not Tri-Automorphic number");
}
}
10//WAP to enter a number and check number is Trimorphic number or not.
import java.util.*;
7
Important Programs: -
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,x,n,c=0,sq;
System.out.println("Enter a number");
n=sc.nextInt();
sq=n*n*n;
for(i=n;i>0;i=i/10)
{
c++;
}
x=sq%(int)Math.pow(10,c);
if(x==n)
System.out.print("Trimorphic number");
else
System.out.print("Not Trimorphic number");
}
}
11//WAP to enter a number and check number is Niven number or not.
import java.util.*;
class coprime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,d,n,s=0;
System.out.println("Enter a number");
n=sc.nextInt();
for(i=n;i>0;i=i/10)
{
d=i%10;
s=s+d;
}
if(n%s==0)
System.out.print("Niven number");
else
System.out.print("Not Niven number");
8
Important Programs: -
}
}
12//WAP to enter a number and check number is Neon number or not.
Input : 9
Output : Neon Number
Explanation: square is 9*9 = 81 and
sum of the digits of the square is 9.
Input :12
Output : Not a Neon Number
Explanation: square is 12*12 = 144 and
sum of the digits of the square is 9 (1
+ 4 + 4) which is not equal to 12.
import java.util.*;
class coprime
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int i,d,n,s=0,sq;
System.out.println("Enter a number");
n=sc.nextInt();
sq=n*n;
for(i=sq;i>0;i=i/10)
{
d=i%10;
s=s+d;
}
if(n==s)
System.out.print("Neon number");
else
System.out.print("Not Neon number");
}
}
13//WAP to enter a number and check number is Palindrome number or not.
import java.util.*;
9
Important Programs: -
class coprime
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int i,d,n,r=0;
System.out.println("Enter a number");
n=sc.nextInt();
for(i=n;i>0;i=i/10)
{
d=i%10;
r=r*10+d;
}
if(n==r)
System.out.print("Palindrome number");
else
System.out.print("Not Palindrome number");
}
}
14//WAP to enter a number and check number is Special number or not.
.
import java.util.*;
class coprime
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int i,d,n,s=0,f=1,k;
System.out.println("Enter a number");
n=sc.nextInt();
for(i=n;i>0;i=i/10)
{
10
Important Programs: -
d=i%10;
for(k=1;k<=d;k++)
f=f*d;
s=s+f;
f=1;
}
if(s==n)
System.out.print("Special number");
else
System.out.print("Not Special number");
}
}
15//WAP to enter a number and check number is Magic number or not.
import java.util.*;
class coprime
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int i,d,n,s=0;
System.out.println("Enter a number");
n=sc.nextInt();
while(n>9)
{
for(i=n;i>0;i=i/10)
{
d=i%10;
s=s+d;
}
n=s;
s=0;
}
if(n==1)
System.out.print("Magic number");
11
Important Programs: -
else
System.out.print("Not Magic number");
}
}
16//WAP to enter a number and check number is Happy number or not.
32 + 22 + 02 ⇒ 9 + 4 + 0 = 13
12 + 32 ⇒ 1 + 9 = 10
12 + 02 ⇒ 1 + 0 = 1
Hence, 320 is a Happy Number.
import java.util.*;
class coprime
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int i,d,n,s=0;
System.out.println("Enter a number");
n=sc.nextInt();
do
{
for(i=n;i>0;i=i/10)
{
d=i%10;
s=s+d*d;
}
n=s;
s=0;
}while(n>9);
if(n==1)
System.out.print("Happy number");
else
System.out.print("Not Happy number");
}
}
17//WAP to enter a number and check number is spy number or not.
12
Important Programs: -
Sum of digits of 1124:
=1+1+2+4
=8
Product of digits of 1124:
=1x1x2x4
=8
As the sum of digits and product of digits is equal for 1124, hence it is a Spy Number.
import java.util.*;
class coprime
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int i,d,n,s=0,p=1;
System.out.println("Enter a number");
n=sc.nextInt();
for(i=n;i>0;i=i/10)
{
d=i%10;
s=s+d;
p=p*d;
}
if(s==p)
System.out.print("spy number");
else
System.out.print("Not spy number");
}
}
18//WAP to enter a number and check number is Duck number or not.
import java.util.*;
class coprime
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int i,d,n,p=1;
System.out.println("Enter a number");
n=sc.nextInt();
for(i=n; i>0; i=i/10)
{
13
Important Programs: -
d=i%10;
p=p*d;
}
if(p==0)
System.out.print("Duck number");
else
System.out.print("Not Duck number");
}
}
19//WAP to enter a number and check number is Preonic number or not.
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
import java.util.*;
class pronic
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int i,n,f=0;
System.out.println("Enter a number");
n=sc.nextInt();
for(i=1; i<=n; i++)
{
if(i*(i+1)==n)
{
f=1;
break;
}
}
if(f==1)
System.out.print("Pronic number");
else
System.out.print("Not Pronic number");
}
}
20//WAP to enter a number and check number is Adam number or not.
import java.util.*;
14
Important Programs: -
class Adam
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int i,n,sq,d,r=0,sq1,r1=0;
System.out.println("Enter a number");
n=sc.nextInt();
sq=n*n;
for(i=sq; i>0; i=i/10)
{
d=i%10;
r=r*10+d;
}
sq1=r*r;
for(i=sq1; i>0; i=i/10)
{
d=i%10;
r1=r1*10+d;
}
if(r1==sq)
System.out.print("Adam number");
else
System.out.print("Not Adam number");
}
}
Example:
Consider the number 12.
Factors of 12 = 1, 2, 3, 4, 6 Sum of factors = 1 + 2 + 3 + 4 + 6 = 16
As 16 > 12 so 12 is an Abundant number.
import java.util.*;
class Adam
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int i,n,s=0;
System.out.println("Enter a number");
n=sc.nextInt();
for(i=1; i<n; i++)
{
if(n%i==0)
s=s+i;
}
if(s>n)
15
Important Programs: -
System.out.print("Abandant number");
else
System.out.print("Not Abandant number");
}
}
22//WAP to enter a number and check number is Deficient number or not.
For example, the number 21 with its proper divisors (1, 3 and 7) has sum (11)
lesser than itself.
import java.util.*;
class Adam
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int i,n,s=0;
System.out.println("Enter a number");
n=sc.nextInt();
for(i=1; i<n; i++)
{
if(n%i==0)
s=s+i;
}
if(s<n)
System.out.print("Deficient number");
else
System.out.print("Not Deficient number");
}
}
23//WAP to enter a number and check number is Strontio numbers or not.
1386*2=2772, we observe that at tens and hundreds place digits are the same.
Hence, 1386 is a strontio number. 1221*2=2442, digits at tens and hundreds place are the
same. Hence, 1221 is a strontio number.
import java.util.*;
public class StrontioNumber
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
16
Important Programs: -
int num=sc.nextInt();
int n=num;
num=(num*2%1000)/10;
if(num%10==num/10)
System.out.println(n+ " is a strontio number.");
else
System.out.println(n+ " is not a strontio number.");
}
}
import java.util.*;
public class XylemPhloem
{
public static void main(String args[])
{
int num, extreme_sum = 0, mean_sum = 0, n;
Scanner sc= new Scanner (System.in);
System.out.print("Enter a number: ");
num = sc.nextInt();
num = Math.abs(num);
n = num;
while(n != 0)
{
if(n == num || n < 10)
17
Important Programs: -
extreme_sum = extreme_sum + n % 10;
else
mean_sum = mean_sum + n % 10;
n = n / 10;
}
if(extreme_sum == mean_sum)
System.out.println(num + " is a xylem number.");
else
System.out.println(num + " is a phloem number.");
}
}
import java.util.Scanner;
public class UniqueNumber
{
public static void main(String args[])
{
int r1, r2, number, num1, num2, count = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number you want to check: ");
number = sc.nextInt();
num1 = number;
num2 = number;
while (num1 > 0)
{
r1 = num1 % 10;
while (num2 > 0)
{
18
Important Programs: -
r2 = num2 % 10;
if (r1 == r2)
{
count++;
}
num2 = num2 / 10;
}
num1 = num1 / 10;
}
if (count == 1)
{
System.out.println("The number is unique.");
}
else
{
System.out.println("The number is not unique.");
}
}
}
26//WAP to enter a number and check number is Keith number or not.
Input : x = 197
Output : Yes
197 has 3 digits, so n = 3
The number is Keith because it appears in the special
sequence that has first three terms as 1, 9, 7 and
remaining terms evaluated using sum of previous 3 terms.
1, 9, 7, 17, 33, 57, 107, 197, .....
import java.util.Scanner;
public class Example26
{
19
Important Programs: -
for(i=d-1; i>=0; i--)
{
arr[i]=n1 % 10;
n1=n1/10;
i=d; sum=0;
while(sum<n)
{
sum = 0;
for(int j=1; j<=d; j++)
{
sum=sum+arr[i-j];
}
arr[i]=sum;
i++;
}
if(sum==n)
System.out.println("Keith Number");
else
System.out.println("Not a Keith Number");
}
}
20
Important Programs: -
{
t=f;
while(t!=0)
{
d=t%10;
s1+=d;
t/=10;
}
n=n/f;
}
else
f++;
}
t=m;
while(t!=0)
{
d=t%10;
s2+=d;
t/=10;
}
if(s1==s2)
System.out.println(“Smith Number”);
else
System.out.println(“Not a Smith Number”);
}
}
28//WAP to enter a number and check number is tech number or not.
A tech number can be tech number if its digits are even and the number of
digits split into two number from middle then add these number if the added
number’s square would be the same with the number it will called a Tech
Number.
import java.util.Scanner;
public class TechNumber
{
public static void main(String[] args)
{
int n,num, x, y, digits = 0,sq,s;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
n = sc.nextInt();
num = n;
while (num > 0)
{
digits++;
num = num / 10;
}
if (digits % 2 == 0)
{
x = n % (int) Math.pow(10, digits / 2);
y = n/ (int) Math.pow(10, digits / 2);
21
Important Programs: -
s=x+y;
sq=s*s;
if (n == sq)
{
System.out.println(n + " is a Tech Number");
}
else
{
System.out.println(n + " is not a Tech Number");
}
}
else
{
System.out.println(n + " Not Tech Number");
}
}
}
29//Write a Program in Java to input a number and check whether it is a
Fascinating Number or not.
Fascinating Numbers: Some numbers of 3 digits or more exhibit a very
interesting property. The property is such that, when the number is multiplied
by 2 and 3, and both these products are concatenated with the original number,
all digits from 1 to 9 are present exactly once, regardless of the number of
zeroes.
Let's understand the concept of Fascinating Number through the following
example:
Consider the number 192
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
Concatenating the results: 192 384 576
It could be observed that '192384576' consists of all digits from 1 to 9 exactly
once. Hence, it could be concluded that 192 is a Fascinating Number. Some
examples of fascinating Numbers are: 192, 219, 273, 327, 1902, 1920, 2019 etc.
import java.util.*;
public class JavaExample
{
public static void main(String args[])
{
int num, multiply2, multiply3;
Scanner sc=new Scanner(System.in);
System.out.print("Enter any Number: ");
num = sc.nextInt();
multiply2 = num * 2;
multiply3 = num * 3;
String str = num + "" + multiply2 + multiply3;
22
Important Programs: -
boolean flag = true;
for(char ch = '1'; ch <= '9'; ch++)
{
int count = 0;
for(int i = 0; i < str.length(); i++)
{
char ch2 = str.charAt(i);
if(ch2 == ch)
count++;
}
if(count > 1 || count == 0)
{
flag = false;
break;
}
}
if(flag)
System.out.println(num + " is a fascinating number.");
else
System.out.println(num + " is not a fascinating number.");
}
}
30//Write a Program in Java to input a number and check whether it is a Ugly
Number or not.
A number is said to be an Ugly number if positive numbers whose prime
factors only include 2, 3, 5.
import java.util.Scanner;
public class UglyNumber
{
public static void main(String[] args)
{
int n;
boolean flag=true;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
while (n != 1)
{
if (n % 5 == 0)
{
n /= 5;
}
else if (n % 3 == 0)
23
Important Programs: -
{
n /= 3;
}
else if (n % 2 == 0)
{
n /= 2;
}
else
{
flag=false;
break;
}
}
if (flag)
{
System.out.println("Ugly number.");
}
else
{
System.out.println("Not Ugly number.");
}
}
}
24