Programming Examples

Java program to input an integer and check whether it is perfect, abundant or deficient number


Write a program to input an integer and check whether it is perfect, abundant or deficient number. If the sum of the factors excluding itself is equal to that number it is perfect, if greater than that number it is abundant and if less than that number it is deficient number.

Solution

class PerfectAbundantDeficient
{
	public static void main(Strign arr[])
	{
		Scanner sc=new Scanner(System.in);
		int i,n,s=0;
		System.out.println("Enter an integer:");
		n=sc.nextInt();
		for(i=1;i<n;i++)
		{
			if(n%i==0)
			s+=i;
		}
		if(s==n)
			System.out.println("Perfect Number");
		else if(s>n)
			System.out.println("Abundant Number");
		else
			System.out.println("Deficient Number");	
	}
}
Output

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad