List of deficient numbers: In the previous article, we have discussed Java program to Check Abundant Number
In this article we are going to understand what Deficient number is and how we can check whether a number is Deficient or not in Java with examples.
Program to Check Deficient Number
Deficient numbers are numbers whose sum of all proper divisors is less than the number.
Example : Deficient Number 15: Divisors- 1,3,5; Sum = 1+3+5 = 9<15 Not a Deficient Number 12: Divisors- 1,2,3,4,6; Sum = 1+2+3+4+6 = 16>12 40: Divisors- 1,2,4,5,8,10,20; Sum = 1+2+4+5+8+10+20 = 50>40
In the above examples the numbers 40 ad 15 are Deficient numbers as sum of their divisors are lesser than the number itself. Hence 12 is not a Deficient number.
Let’s see different ways to check deficient number.
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Approach :
- Enter/declare a number and store it .
- Then we find the divisors of the number ad add them up.
- If the resultant sum is greater than the number itself, then the number is said to be a Deficient number.
Method-1: Java Program to Check Deficient Number By Using Static Value
import java.util.Scanner; public class DeficientNumber{ public static void main(String args[]) { //A number declared int num = 15; int sum = 0; // Loop to find the divisors as well as add them up for(int i = 1; i<num; i++) if(num%i==0) sum+=i; if(sum<num) { System.out.println(num+" is a Deficient number"); } else { System.out.println(num+" is Not a Deficient number"); } } }
Output: 15 is a Deficient number
Method-2: Java Program to Check Deficient Number By User Input Value
import java.util.*; public class DeficientNumber { public static void main(String args[]) { //Taking the number as input from the user using scaner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); int sum = 0; // Loop to find the divisors as well as add them up for(int i = 1; i<num; i++) if(num%i==0) sum+=i; if(sum<num) { System.out.println(num+" is a Deficient number"); } else { System.out.println(num+" is Not a Deficient number"); } } }
Output: Enter a number : 15 15 is a Deficient number
Method-3: Java Program to Check Deficient Number By User Defined Method
import java.util.*; public class DeficientNumber { public static void main(String args[]) { //Taking the number as input from the user using scaner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //Calling checkNumber() method to check Deficient number checkNumber(num); } public static void checkNumber(int num) { int sum = 0; // Loop to find the divisors as well as add them up for(int i = 1; i<num; i++) if(num%i==0) sum+=i; if(sum<num) { System.out.println(num+" is a Deficient number"); } else { System.out.println(num+" is Not a Deficient number"); } } }
Output: Enter a number : 15 15 is a Deficient number
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Related Java Programs: