Tuesday, June 4, 2013

Program to check whether a number is Deficient number, Perfect number or Abundant number

PROGRAM
Suppose you take a positive integer n and add its (positive) divisors. E.g.  if n=18, then sum=1+2+3+6+9+18=39. In general, when we do this with n, one of the following three happens:
If the sum is
n is said to be
e.g.
Less than 2n
Deficient number
1,2,3,4,5,8,9
Equal to 2n
Perfect number
6,28,496
Greater than 2n
Abundant number
12,18,20,24,30

WAP that inputs a number and determines whether it is a Deficient number or a Perfect number or a Abundant number.
SAMPLE DATA

Enter a no: 28                                    It is a Perfect number
Enter a no: 30                                    It is an Abundant number
--------------------------------------------------------------------------------------------------------

/**
 * class checknumber checks whether a number is Deficient number,
 * Perfect number or Abundant number.
 * @author: Nitendra Kumar 
 * @version: June 5,2013
 */
import java.util.Scanner;
public class checknumber
{
    public int num=0;
    public void getnum()  //method to accept a number from user
    {
        Scanner in=new Scanner(System.in);  //creting an object of Scanner class
        System.out.println("Enter a number");
        num=in.nextInt();
    }
    public void fnchecknumber()  //method to check type of number
    {
        int sum=0,dnum=0;
        for(int i=1;i<=num;i++)  
        {
            if(num%i==0)
              sum+=i;
        }
        dnum=2*num;
        if(sum<dnum)
          System.out.println("It is a Deficient number");
        else if(sum==dnum)  
          System.out.println("It is a Perfect number");
        else if(sum>dnum)
          System.out.println("It is an Abundant number");
    }
    
    public static void main()   //Entry point of program
    {
        checknumber ob=new checknumber();  //creating object of class checknumber
        ob.getnum();                       //calling method
        ob.fnchecknumber();          //calling method
    }
}

OUTPUT:
Enter a number
30
It is an Abundant number


/*---------------Program developed by: Nitendra Kumar---------------*/
//For more details visit http://javawithnitendra.blogspot.in


                      For eBook on Language C click here..

6 comments:

  1. Easy 2 understand....Thanks

    ReplyDelete
  2. this is a straight forward approach. Aren't there any efficient approaches

    ReplyDelete
  3. hi can u make a program if i enter a number it will define if it is abundant or not like this
    enter a number : 12
    abundant is : 4

    ReplyDelete

Ur comments r most welcome...