Computer Applications

Write a Java Program to define a function which accept a number and check it is a perfect number or not

Write a Java Program to define a function which accept a number and check it is a perfect number or not.

import java.io.*;
import java.util.*;

class Perfect
{
	void check( int n )
	{
		int s = 0;
		for( int i = 1; i < n ; i++)
		{
			 if(n%i==0)
			 	s = s + i ;
		}
		if( s == n )
		  System.out.println(" Number is Perfect.");
		else
		  System.out.println(" Number is not Perfect.");
	}
  public static void main(String args[ ])
  {
  	Scanner sc = new Scanner(System.in);
  	int n;
  	System.out.println("Enter n =");
  	n = sc.nextInt();
  	Perfect ob = new Perfect( );
  	ob.check( n );
  }
}

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top