source file name: NumberDemo.java
class A
{
int y;
public A(int n)
{
y=n;
}
public void divdisp()
{
int d=1;
while(d<y)
{
if(y%d==0)
System.out.print(d+" ");
d++;
}
}
public void perfectno()
{
int d=1;
int sum=0;
while(d<y)
{
if(y%d==0)
sum=sum+d;
d++;
}
if(sum==y)
System.out.println("\n"+y+" number perfect no");
else
System.out.println("\n"+y+" number is not perfect no");
}
}
class NumberDemo
{
public static void main(String as[])
{
int n=Integer.parseInt(as[0]);
A ob=new A(n);
System.out.println("divisors are:");
ob.divdisp();
ob.perfectno();
}
}
output :
D:\JavaPrograms\Constructors>javac NumberDemo.java
D:\JavaPrograms\Constructors>java NumberDemo 14
divisors are:
1 2 7
14 number is not perfect no
D:\JavaPrograms\Constructors>java NumberDemo 28
divisors are:
1 2 4 7 14
28 number perfect no
0 Comments
Post a Comment