0% found this document useful (0 votes)
12 views8 pages

Logic Problems: Armstrong to Abundant

The document outlines several logic building problems related to number classification, including Armstrong numbers, Strong numbers, Friendly pairs, and Abundant numbers. Each section provides a definition, example, and Java code implementation for checking the respective number type. Additionally, explanations of the code logic and structure are included for better understanding.

Uploaded by

shravanmb163
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Logic Problems: Armstrong to Abundant

The document outlines several logic building problems related to number classification, including Armstrong numbers, Strong numbers, Friendly pairs, and Abundant numbers. Each section provides a definition, example, and Java code implementation for checking the respective number type. Additionally, explanations of the code logic and structure are included for better understanding.

Uploaded by

shravanmb163
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Logic Building Problems

[Link] Submission
Student Name: Praveen Biradar
ID: KODD8KS8I

[Link] a number as input and check whether it is an Armstrong number or not.

An Armstrong Number (also called a Narcissistic Number) is a number where the sum of its digits, each
raised to the power of the total number of digits, equals the original number.

Example:

1. 153 (3-digit number)


13+53+33=1+125+27=1531^3 + 5^3 + 3^3 = 1 + 125 + 27 = 15313+53+33=1+125+27=153

Since the sum is equal to the original number, 153 is an Armstrong number.

1. package logicBuildingExamplesLevel_2;
2.
3. import [Link];
4.
5. public class ArmStrongNumberOrNot {
6. public static void main(String[] args) {
7. Scanner sc = new Scanner([Link]);
8. [Link]("Enter a number: ");
9. int num = [Link]();
10.
11. int result = [Link](num);
12.
13.
14. [Link]("Calculated sum: " + result);
15.
16. // Check if it's an Armstrong number
17. if (result == num) {
18. [Link](num + " is an Armstrong number.");
19. } else {
20. [Link](num + " is not an Armstrong number.");
21. }
22.
23. [Link]();
24. }
25. }
package logicBuildingExamplesLevel_2;

public class ArmStrongNumberOrNot1 {

// Static method to check Armstrong number


public static int Armstrong(int a) {
int s = 0;
int temp = a;
int numDigits = [Link](a).length(); // Count number of digits

while (temp != 0) {
int digit = temp % 10;
s += [Link](digit, numDigits); // Raise to the correct power
temp = temp / 10; // Remove last digit
}
return s;
}
}

Explaination:
import [Link];

This imports the Scanner class, which is used to take user input.

public class DigitCountExample {


public static void main(String[] args) {

This defines the main class and method where the program starts execution.

Scanner sc = new Scanner([Link]);


[Link]("Enter a number: ");
int num = [Link]();

This creates a Scanner object, prompts the user to enter a number, and stores it in num.

int numDigits = [Link](num).length();


[Link]("Number of digits: " + numDigits);

This converts num to a string, counts the number of digits, and prints the result.

[Link]();

This closes the Scanner to free system resources.


2. Take a number as input and check whether it is an Strong number or not.

A Strong Number is a number where the sum of the factorials of its digits is equal to the original number.

Example:

1. 145

1!+4!+5!=1+24+120=1451! + 4! + 5! = 1 + 24 + 120 = 1451!+4!+5!=1+24+120=145

package logicBuildingExamplesLevel_2;

import [Link];

public class StrongNUmberOrNot {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link](); // Read user input

StrongNUmberOrNot1 obj = new StrongNUmberOrNot1(); // Create object of second class


boolean isStrong = [Link](n); // Call method to check Strong Number

if (isStrong) {
[Link](n + " is a Strong Number.");
} else {
[Link](n + " is not a Strong Number.");
}

[Link](); // Close Scanner


}
}

package logicBuildingExamplesLevel_2;

public class StrongNUmberOrNot1 {


public boolean isStrong(int n) {
int originalNum = n; // Store original number
int sum = 0;

while (n > 0) {
int digit = n % 10; // Extract last digit
sum += factorial(digit); // Compute factorial and add to sum
n /= 10; // Remove last digit
}

return sum == originalNum; // Check if sum equals original number


}

private int factorial(int num) {


int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i; // Compute factorial
}
return fact;
}
}

Explanation of StrongNUmberOrNot1

This class checks if a number is a Strong Number.

Method: isStrong(int n)

1. Stores the original number for comparison.


2. Extracts each digit using n % 10.
3. Computes the factorial of the digit using factorial(digit).
4. Adds the factorial sum and removes the last digit (n /= 10).
5. If sum == originalNum, returns true (Strong Number), else false.

Method: factorial(int num)

• Computes the factorial of a number using a loop.

Example:

For 145:
1! + 4! + 5! = 1 + 24 + 120 = 145 → Strong Number
3. Take 2 input and check whether it is an Friendly Pair number or not.

A friendly pair, also known as amicable numbers, is a pair of positive integers where the sum of the proper divisors
of one number equals the other number, and vice versa. Proper divisors are all the divisors of a number excluding
the number itself.

Example

For input numbers 220 and 284:

• Sum of proper divisors of 220 = 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284


• Sum of proper divisors of 284 = 1 + 2 + 4 + 71 + 142 = 220

package logicBuildingExamplesLevel_2;

import [Link];

public class FriendlyPairs {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();

boolean isFriendly = [Link](num1, num2);

if (isFriendly) {
[Link](num1 + " and " + num2 + " are friendly pairs.");
} else {
[Link](num1 + " and " + num2 + " are not friendly pairs.");
}

[Link]();
}
}

package logicBuildingExamplesLevel_2;

public class FriendlyPairsUtility {


// Compute the sum of proper divisors for a given number
public static int sumOfProperDivisors(int n) {
int sum = 1; // 1 is a proper divisor for any n > 1
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) {
sum += i;
if (i != n / i) {
sum += n / i;
}
}
}
return sum;
}

// Check if two numbers are friendly pairs (amicable numbers)


public static boolean isFriendlyPair(int a, int b) {
if (a <= 0 || b <= 0) {
return false;
}
return sumOfProperDivisors(a) == b && sumOfProperDivisors(b) == a;
}
}

How It Works

1. [Link] (Main Class):


o Prompts the user to enter two numbers.
o Calls the utility method isFriendlyPair from the second file to check if the numbers are friendly.
o Prints the result.
2. [Link] (Utility Class):
o Contains the method sumOfProperDivisors to calculate the sum of all proper divisors of a
number.
o Contains the method isFriendlyPair that uses the sum of proper divisors to determine if two
numbers form a friendly (amicable) pair.
4. Take a number as input and check whether it is an Abundant number or not.
An abundant number is a positive integer for which the sum of its proper divisors (all divisors excluding the
number itself) is greater than the number.

For example, consider the number 12.

Its proper divisors are 1, 2, 3, 4, and 6, and their sum is 16, which is greater than 12. Hence,

12 is an abundant number.

package logicBuildingExamplesLevel_2;

import [Link];

public class AbundantNumberCheck {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

// Use the utility class to check if the number is abundant.


if ([Link](num)) {
[Link](num + " is an abundant number.");
} else {
[Link](num + " is not an abundant number.");
}

[Link]();
}
}

package logicBuildingExamplesLevel_2;

public class AbundantNumberUtility {

// Method to calculate the sum of proper divisors of a number.


public static int sumOfProperDivisors(int n) {
int sum = 0;
// Loop from 1 to n/2, since no proper divisor can be greater than n/2.
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
sum += i;
}
}
return sum;
}

// Method to check if a number is abundant.


public static boolean isAbundant(int n) {
return sumOfProperDivisors(n) > n;
}
}
How It Works

1. [Link] (Main Class)


o Prompts the user to enter a number.
o Reads the input and passes it to the utility method isAbundant() from AbundantNumberUtility.
o Displays whether the number is abundant based on the result.
2. [Link] (Utility Class)
o Contains the sumOfProperDivisors(int n) method, which calculates the sum of all proper divisors
(divisors less than the number itself).
o Contains the isAbundant(int n) method that returns true if the sum of proper divisors is greater than
the number, indicating it is abundant.

Compile both files (ensuring they are in the same package/directory) and run the main class to check for
abundant numbers.

Common questions

Powered by AI

Checking Armstrong and Strong numbers entails iterating through the digits of a number and performing calculations (power or factorial) for each digit, making these algorithms dependent on the number of digits (O(d)) and on the cost of power or factorial operations per digit. In contrast, checking for Abundant numbers involves finding proper divisors up to n/2, resulting in a higher complexity of O(n/2) due to iteration across potential divisors. The abundance check is less efficient at higher numbers compared to digit-based operations typically found in Armstrong and Strong number verification. The complexity in checking Strong numbers is compounded by factorial calculations for each digit .

The Java Scanner class facilitates taking user input, critical for interactive applications requiring numbers from users. It is utilized to initialize the Scanner object for reading system input, handling integers with methods like nextInt(). In the presented examples, Scanner is employed to capture numbers input by users and process them through arithmetic checks such as determining if a number is Armstrong, Strong, or an Abundant number, thus illustrating fundamental input-output operations in Java .

Utility classes in Java encapsulate reusable code, aiding in organized and efficient project structure. In examples, utility classes compute divisors, validate number properties (e.g., Armstrong or Abundant checks), and return results. This modularization isolates logic, simplifies main class tasks, enhances maintainability, and encourages code reuse across different parts of a program or other projects, emphasizing clarity and decomposition in programming design .

One potential improvement is optimizing the divisor-sum calculation for Friendly and Abundant number checks by using a method that quickly identifies divisors through known mathematical properties, reducing unnecessary checks. For instance, leveraging the symmetry property of divisors can minimize operations (checking only up to √n), thus reducing computational overhead and improving execution time. Applying memoization for factorial computations in Strong numbers can prevent duplicate calculations, enhancing speed. These changes could significantly enhance performance, especially for larger numbers, without compromising accuracy .

A Friendly Pair, or amicable numbers, are two numbers where each number's sum of proper divisors equals the other number. To verify this, calculate the sum of all proper divisors of both numbers, excluding the numbers themselves. If each sum matches the other number, they form a Friendly Pair. For instance, numbers 220 and 284 form a Friendly Pair since the sum of 220's divisors is 284 and vice versa .

A Java program can determine if a number is an Armstrong number by creating a Scanner to take user input, extracting each digit, computing the Armstrong condition (each digit raised to the power of total digits and summed), and comparing the result to the original number. The logic involves looping through the digits, using Math.pow(), and checking if the computed sum equals the input number. The provided code exemplifies this by defining a class with a static method to perform these calculations and output the result .

A Strong number is identified by the sum of the factorials of its digits equaling the original number. The method involves extracting each digit with n % 10, calculating the factorial of each digit, summing all those factorials, and comparing the total sum to the original number. For example, for number 145: 1! + 4! + 5! = 1 + 24 + 120 = 145, which shows it is a Strong number .

An Armstrong number, also known as a Narcissistic Number, is a number where the sum of its digits, each raised to the power of the total number of digits, is equal to the original number. To determine if a number is an Armstrong number, you convert the number to its digits, count the number of digits, compute the sum of each digit raised to the count of digits, and check if this sum equals the original number .

An Abundant number is a positive integer where the sum of its proper divisors (excluding the number itself) is greater than the number itself. To check for abundance, calculate the sum of proper divisors for a number using divisors less than itself (up to half the number), then compare this sum with the number. If the sum exceeds the number, it is abundant .

Friendly Pairs are considered rare due to the specific requirement that each number in the pair has a sum of proper divisors equal to the other number, a property not commonly met between random pairs. Computationally, checking each possible pair for the sum condition across possible divisors is intensive, requiring O(n) operations to calculate divisors for each candidate number. This rarity and computational demand make identifying Friendly Pairs challenging, as seen with the classic examples like 220 and 284 .

You might also like