We are uploading our contents, please refresh app.
Java Premium

write a program in java to accept a number from the user and check whether it is a Perfect number or not using impure (non-return) function.

Perfect number – a number is called a Perfect number if it is equal to the sum of its factors other
than the number itself. Example: 6 = 1 + 2 + 3.

Program:
import java.util.*;
class Perfect_Imp_fun
{
  public void fun(int n)
  {
    int s=0,i;
    for(i=0;i<n;i++)
{
  if(i%2==0)
    s=s+i;
}
if(n==s)
{
  System.out.println("Entered number is perfect");
}
else
 System.out.println("Entered number is not perfect"); 
}
  public static void main(String[] args) 
{
int a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
a=sc.nextInt();
Perfect_Imp_fun obj=new Perfect_Imp_fun();
obj.fun(a);
}
}

Output:
Enter the number: 
6
Entered number is perfect.

Enter the number: 
6
Entered number is not perfect.

Online user

Download our android app Download App Submit your query
x

Get Updates On

Java & Premium Programs

Java Solved & Questions

Java Coding easy to use

Proper Coding

Straight Into Your INBOX!

We are going to send you our resources for free. To collect your copy at first, join our mailing list. We are promising not to bother you by sending useless information. So don't miss any updates, stay connected!