Abundant Number or not in Java
Check Whether or Not the Number is an Abundant Number in Java
Given an integer input, the objective is to check whether the sum of all the factors of a number except the number itself is greater than the number or not. Therefore, we’ll write a code to Check Whether or Not the Number is an Abundant Number in Java.
Example
Input : 18
Output : Yes, it's an Abundant Number
Check Whether or Not the Number is an Abundant Number in Java Language
Given an integer input, the objective is to check whether or not the sum of all the factors of the number, except the number itself is greater than the number. For any number to be an Abundant Number, the sum of it’s factors except itself must be greater than the number. Therefore, we’ll first find all the factors of the number without the number itself within the range 1 to the number/2. We’ll keep summing the factors meanwhile and finally check with the actual number. If we Include the number itself as a factors then the sum must be divided by 2 before comparing. Here are some of the methods to Check Whether or Not the Number is an Abundant Number in Java Language
- Method 1: Using Range until Number
- Method 2: Using Range until Sqrt( Number )
We’ll discuss the above mentioned methods in detail in the upcoming sections. Do check out the blue box given below for better understanding of the above mentioned questions.
Let's try and understand the concept better using an example
Example Input : Number = 18 Output : Yes, It's an Abundant Number Explanation : The Factors for the number 18 are, 1, 2, 3, 6 and 9. We don't want to include the number itself. Now the sum of the factors except the number itself is : 1 + 2 + 3 + 6 + 9 = 21 as the number 21>18 , the number itself. It's an abundant number.We'll discuss on how to check for an Abundant Number in the upcoming sections.
Method 1: Using Range until Number
Java Code
public class Main { public static void main (String[]args) { int n = 12, sum = 0; for (int i = 1; i < n; i++) { if (n % i == 0) sum = sum + i; } if (sum > n) { System.out.println (n + " is an Abundant Number"); System.out.println ("The Abundance is: " + (sum - n)); } else System.out.println (n + " is not an Abundant Number"); } }
Output
12 is an Abundant Number The Abundance is: 4
Method 2: Using Range until sqrt( Number )
Java Code
public class Main { public static void main (String[]args) { int n = 12; int sum = getSum(n); if(sum > n) { System.out.println (n + " is an Abundant Number"); System.out.println ("The Abundance is: " + (sum - n)); } else System.out.println (n + " is not an Abundant Number"); } static int getSum(int n){ int sum = 0; for(int i = 1; i < Math.sqrt(n); i++) { if (n % i == 0) { // For n : (1, n) will always be pair of divisor // acc to def., we must ignore adding n itself as divisor // when calculating for abundant number if(i == 1) sum = sum + i; // Example For 100 (10,10) will be one of the pair // But, we should add value 10 to the sum just once else if(i == n/i) sum = sum + i; // add both pairs as divisors // For any divisor i, n/i will also be a divisor else sum = sum + i + n/i; } } return sum; } }
Output
12 is an Abundant Number The Abundance is: 4
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
public class AbundantNumber {
public static void Abundant(int num)
{
int temp=num;
int sum=0;
for(int i=1;itemp)
{
System.out.print(“is Abundant”);
}
else
{
System.out.print(“not”);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int num=18;
Abundant(num);
}
}
Hey there, Kindly join our Discord server for all the technical and subject related queries.
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
for(int i=1;in)
System.out.println(n+” is Abundant Number.”);
else
System.out.println(n+” is Not Abundant Number.”);
}
}
Hey, join our Discord Channel for technical queries
class HelloWorld {
public static void main(String[] args) {
int num =18;int sum =0;
for(int i = 1; i num)
System.out.println(“true abundance”);
else
System.out.println(“No abundance”);
}
}
class HelloWorld {
public static void main(String[] args) {
int n=12;
int sum=0;
for(int i=1;in)
System.out.println(“Ab”);
else
System.out.println(“No Ab”);
}
}
class HelloWorld {
public static void main(String[] args) {
int n=12;
int sum=0;
for(int i=1;in)
System.out.println(“Ab”);
else
System.out.println(“No Ab”);
}
}
class HelloWorld {
public static void main(String[] args) {
int num=18;
int sum=0;
for(int i=1;inum){
System.out.println(“Abundant Number”);
}
else
System.out.println(“not abundant Number”);
}
}
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
for(int i=n;i>1;i–)
{if(n%i==0)
{sum=sum+i;}}
if(sum>n)
System.out.println(“yesh”);
else
System.out.println(“not”);
}}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Num = sc.nextInt();
int Sum = 0;
int temp = Num;
for (int i = 1; iNum) {
System.out.println(“Abudant Number”);
} else {
System.out.println(“Not an Abudant Number”);
}
}
}
//An abundant number is a number for which the sum of its proper factors is greater than the number itself.
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
System.out.print(“Enter a no.: “);
int no = sc.nextInt();
int num = no;
for(int i=1; ino){ //if the calculated sum is greater than given number, then number is abundant
System.out.println(no+ ” is abundant.”);
int abu = sum – no; //Calculating the abundance factor
System.out.println(“Its abundance is: “+ abu);
}
else
System.out.println(“No is not abundant”);
}
}