0% found this document useful (0 votes)
8 views20 pages

Java Programs for Number Classification

The document contains a series of Java programs that check various properties of numbers, including prime, perfect, abundant, Armstrong, palindrome, automorphic, deficient, disarium, duck, evil, happy, harshad, kaprekar, Krishnamurthi, lead, lucky, narcissistic, neon, pronic, and spy numbers. Each program prompts the user to input a number and then determines if the number meets the specified criteria, printing the result accordingly. The code examples include various logical structures and mathematical operations to achieve the desired checks.

Uploaded by

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

Java Programs for Number Classification

The document contains a series of Java programs that check various properties of numbers, including prime, perfect, abundant, Armstrong, palindrome, automorphic, deficient, disarium, duck, evil, happy, harshad, kaprekar, Krishnamurthi, lead, lucky, narcissistic, neon, pronic, and spy numbers. Each program prompts the user to input a number and then determines if the number meets the specified criteria, printing the result accordingly. The code examples include various logical structures and mathematical operations to achieve the desired checks.

Uploaded by

santwanabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM 1:- WRITE A PROGRAM TO ACCEPT A

NUMBER AND CHECK IT IS A PRIME NUMBER OR NOT

import [Link].*;
class primenumber
{
public static void main(String[]args)
{
Scanner sc=new Scanner([Link]);
[Link](“Enter a number”);
int n=[Link] Int( );
for( int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
[Link](n+“ is a Prime Number”);
else
[Link](n+“ is not a Prime Number”);
}
}
PROGRAM 2:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND CHECK IT IS A PERFECT NUMBER OR
NOT

import java .util.*;


class perfectnumber
{
public static void main(String [] args)
{
Scanner sc=new Scanner([Link]);
[Link](“Enter a number”);
int n=[Link] Int();
for (int i = 1;i<=n;i++)
{
if (n%i==0)
c=c+i;
}
if (c==n)
[Link](n+“ is a perfect number”);
else
[Link](n+” is not a perfect number”);
}
}
WRITE A PROGRAM TO INPUT A NUMBER AND CHECK
IT IS A ABUNDANT NUMBER OR NOT
PROGRAM 4:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND DISPLAY THAT IT IS AN ARMSTRONG
NUMBER OR NOT.
import [Link].*;
class armstrongnumber
{
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
[Link]("Enter a number=");
int n=[Link]();
int r=0, s=0, k==n;
while(n>0)
{
r=n%10;
s=s+(r*r*r);
n=n\10;
}
if(k==s)
[Link](k+"is an armstrong number");
else
[Link](k+"is not an armstrong number");
}
}
PROGRAM 5:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND DISPLAY IT IS A PALINODROME
NUMBER OR NOT.
import [Link].*;
class palinodromenumber
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int n= [Link]();
int r=0,s=0,k=n;
while(n>0)
{
r=n%10;
s=(s*10)+r;
n=n/10;
}
if(s==k)
[Link](k+"is a palinodrome number");
else
[Link](k+"is not a palinodrome number");
}
}
PROGRAM 6:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND DISPLAY IT IS A AUTOMORPHIC
NUMBER OR NOT.
import [Link].*;
class automorphicnumber
{
public static void main(String[]args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int n=[Link]();
int sq= n*n;
int c=0,k=sq;
while(n>0)
{
c++;
n=n/10;
int I=(int)(sq%[Link](10,c));
}
if(l==k)
[Link](n+"is an automorphic number");
else
[Link](n+"is not an automorphic number");
}
}
PROGRAM 7:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND CHECK IT IS A DEFICIENT NUMBER OR
NOT.
import [Link].*;
class deficientnumber
(
public static void main(String[]args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number");
int n = [Link]();
int c=0;
for(int i = 1; i <= n; i++)
{
if(n%i==0)
c++;
}
if(c<n)
[Link]("It is a deficient number");
else
[Link]("It is not a deficient number");
}
}
PROGRAM 8:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND CHECK IT IS A DISARIUM NUMBER OR
NOT
import [Link].*;
class disariumnumber
{
public static void main(String[]args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number");
int n = [Link]();
int k = n, r = 0, c = 0, z = k, s = 0;
while(n > 0)
{
c += 1;
n %= 10;
}
while(k > 0)
{
r = k % 10;
s += (int)[Link](r,c);
c--;
k /= 10;
}
if(s == z)
[Link]("It is a disarium number");
else
[Link]("It is not a disarium number");
}
}
PROGRAM 9:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND CHECK IT IS A DUCK NUMBER OR NOT.
import [Link].*;
class ducknumber
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number");
int n = [Link]();
int p = 1, r = 0;
while(n < 0)
{
r = n % 10;
p=r;
n/= 10;
}
if(p == 0)
[Link]("It is a duck number");
else
[Link]("It is not a duck number”);
}
}
PROGRAM 10:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND CHECK IT IS A EVIL NUMBER OR NOT
import [Link].*;
class evilnumber
{
public static void main (String[]args)
{
Scanner sc=new Scanner([Link])
[Link](“Enter a number”);
int n=[Link] Int();
int r=0,c=0;
while(n>0)
{
r=n%2;
if(r==1)
c=c+1;
n=n\2;
}
if (c%2==0)
[Link](“It is a evil number”);
else
[Link](“It is not a evil number”);
}
}
PROGRAM 11:- WRITE A PROGRAM TO ACCEPT A NO
AND CHECK IT IS A HAPPY NUMBER OR NOT:-
import [Link].*;
class happynumber
{
public static void main(String[]args)
{
Scanner sc=new Scanner([Link])
[Link](“Enter a number”);
int n=[Link]();
int r=0,sum=0,k;
sum=n;
while(sum>9)
{
k=sum;
sum=0;
while(k>0)
{
r=k%10;
sum=(sum+r*r);
k=k\10;
}
}
if(sum==1)
[Link](“It is an happy Number”);
else
[Link](“It is an not an happy Number’’);
}
}
PROGRAM 12:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND CHECK IT IS A HARSHAD NUMBER OR
NOT
import [Link].*;
class harshadnumber
{
public static void main(String[]args)
{
Scanner sc=new Scanner([Link]);
[Link](“Enter a number”);
int n=[Link]();
int r=0,s=0;
int k=n;
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
if(k%s==0)
[Link](“It is a harshad number”);
else
[Link](“It is not a harshad number”);
}
}
PROGRAM 13:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND CHECK IT IS A KAPRIKER NUMBER OR
NOT
import [Link].*;
class kaprikernumber
{
public static void main(String[] args)
{
Scanner sc=new Scanner (System. in);
[Link] ("Enter a number");
int n=[Link] int();
int c=0,k=sq;
while(sq>0)
{
c++;
sq=sq/10;
}
Int d=c/2;
int z=(int)[Link](10,d);
int f=k/z;
int l=k%z;
int s=f+l;
if (n==s)
[Link]("It a kapriker number");
else
[Link] ("It is not a kapriker number");
}
}
PROGRAM 14:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND CHECK IT IS KRISHNAMURTI NUMBER
OR NOT
import [Link].*;
{
class krishnamurti
{
public static void main(String[]args)
{
Scanner sc=new Scanner (System. in);
[Link]("enter a number");
int n =[Link] Int();
int k=n;
int r = 0 f = 1, s = 0
while (n > 0)
{
r = n%10;
f = 1;
for (int i=1;<= r,i++)
{
f=f*i;
}
n=n/10;
s=s+f;
}
if(s==k)
[Link]("it is a Krishna murti number");
else
[Link]('It is not a Krishna murti number");
}
}
PROGRAM 15:- WRITE A PROGRAM TO ACCEPT A
NUMBER AND CHECK IT IS A LEAD NUMBER OR NOT
import [Link].*;
class lead
{
public static void main(String[] args)
{
Scanner sc=new Scanner (System. in);
System. out. printin ("enter a number");
int n=[Link] Int ();
int r=0,a=0,b=0;
while (n>0)
{
r=n%10;
if(r%2==0)
a=a+r;
else
b=b+r;
n=n/10;
}
if (a==b)
[Link] ("It is a lead number");
else
[Link]("IT is not a lead number");
}
}
PROGRAM 16:- WRITE A PROGRAM TO INPUT A NUMBER,
CHECK AND PRINT IT IS A LUCKY NUMBER OR NOT
import [Link].*;
class luckynumber
{
public static void main(String [] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int n=[Link]();
int r=0,count=0;
for(int i=0;i<=9;i++)
{
int k=n;
count=0;
while(k>0)
{
r=(k%10);
if(r==i)
count++;
k=(k/10);
}
if(count>1)
break;
}
if(count>1)
[Link](n+" is not a lucky number");
else
[Link](n+" is a lucky number");
}
}
PROGRAM 17:- WRITE A PROGRAM TO INPUT A NUMBER,
CHECK AND PRINT IT IS A NARCISSISTIC NUMBER OR NOT
import [Link].*;
class narcissticnumber
{
public static void main(String []args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int n=[Link]();
int c=0,s=0,r=0,k=n,z=n;
while(n>0)
{
c++;
n=n/10;
}
while(k>0)
{
r=k%10;
s=s+(int)[Link](r,c);
k=k/10;
}
if(z==s)
[Link](s+" is a Narcissistic Number");
else
[Link](s+" is not a Narcissistic Number");
}
}
PROGRAM 18:- WRITE A PROGRAM TO INPUT A NUMBER,
CHECK AND PRINT IT IS A NEON NUMBER OR NOT
import [Link].*;
class neonnumber
{
public static void main(String []args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int n=[Link]();
int sq=n*n;
int r=0,s=0;
while(sq>0)
{
r=sq%10;
s=s+r;
sq=sq/10;
}
if(n==s)
[Link](n+" is a Neon Number");
else
[Link](n+" is not a Neon Number");
}
}
PROGRAM 19:- WRITE A PROGRAM TO ENTER A NUMBER,
CHECK AND PRINT IT IS A PRONIC NUMBER OR NOT
import [Link].*;
class pronicnumber
{
public static void main(String []args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int n=[Link]();
int sq=(int)[Link](n);
int p=sq*(sq+1);
if(p==n)
[Link](n+" is a Pronic Number");
else
[Link](n+" is a not Pronic Number");
}
}
PROGRAM 20:- WRITE A PROGRAM TO INPUT A
NUMBER, CHECK AND PRINT IT IS A SPY NUMBER OR
NOT
import [Link].*;
class spynumber
{
public static void main(String []args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int n=[Link]();
int s=0,r=0,m=1,k=n;
while(n>0)
{
r=n%10;
s=s+r;
m=m*r;
n=n/10;
}
if(s==m)
[Link](k+" is a Spy Number");
else
[Link](k+" is not a Spy Number");
}
}

Common questions

Powered by AI

The program reverses the input number by extracting its digits and appending them to a new number constructed in reverse order. It extracts digits using modulus 10 and builds the reversed number by multiplying the current result by 10 and adding the digit. It checks if the reversed number matches the original number to determine if the number is a palindrome .

The program calculates the sum of the digits of the number and then checks if the original number is divisible by this sum. A Harshad number is significant because it is divisible by the sum of its digits, showing a specific balanced property related to its numeral components .

The program assesses whether a number is a Disarium number by summing each digit raised to the power of its position index (from left to right) and comparing this sum to the original number. It uses a loop to iterate through digits, calculating powers using the Math.pow method and reducing the number until all digits have been processed .

The program takes the square of the number and splits the resulting square into two parts by determining a division point based on the number of digits. It then adds these two parts together. If the sum equals the original number, it identifies it as a Capriciogenic number. This process hinges on significant manipulation of the digits post-squaring .

The program accepts an integer input and uses a for loop to iterate from 1 to the number. For each integer, it checks if the input number is divisible by the current integer (i.e., if n % i == 0). It counts the number of divisors. A prime number has exactly two divisors: 1 and itself. Therefore, if the count of divisors is exactly two, the program declares the number as a prime number, otherwise, it is not prime .

A Spy number's identification involves checking if the sum of its digits is equal to the product of its digits. The program accomplishes this by iterating through the digits, summing them and multiplying them separately in a loop. If these two values are equal, the number is categorized as a Spy number, relying on this unique property .

The program identifies a Duck number by ensuring that the number contains at least one zero in its body but does not start with zero. This is checked by iterating through the digits, ignoring initial zeros, and then looking for zero within the subsequent digits .

The program determines if a number is Pronic by checking if it can be expressed as the product of two consecutive integers. It does this by taking the integer square root of the number, then calculating the product of this number and its successor to see if the original number results. This relies on the property of Pronic numbers being products of consecutive integers .

The program calculates the sum of the cubes of every digit in the number. It retrieves each digit by taking modulus 10 of the number in a loop and then reduces the number by dividing by 10 until no more digits are left. It checks if the calculated sum equals the original number, determining if it's an Armstrong number .

A perfect number is defined as a number that is equal to the sum of its proper divisors. The program checks for perfect numbers by summing the divisors and comparing the sum to the original number. A deficient number, on the other hand, is when the sum is less than the number itself. An abundant number has a sum of divisors that is greater than the number. Each program evaluates the sum of divisors but compares it differently to determine the category .

You might also like