0% found this document useful (0 votes)
5 views

KNA-9 Logic_Building_Problems

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)
5 views

KNA-9 Logic_Building_Problems

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
You are on page 1/ 8

Logic Building Problems

9.Assignment Submission
Student Name: Praveen Biradar
ID: KODD8KS8I

1.Take 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 java.util.Scanner;
4.
5. public class ArmStrongNumberOrNot {
6. public static void main(String[] args) {
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter a number: ");
9. int num = sc.nextInt();
10.
11. int result = ArmStrongNumberOrNot1.Armstrong(num);
12.
13.
14. System.out.println("Calculated sum: " + result);
15.
16. // Check if it's an Armstrong number
17. if (result == num) {
18. System.out.println(num + " is an Armstrong number.");
19. } else {
20. System.out.println(num + " is not an Armstrong number.");
21. }
22.
23. sc.close();
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 = String.valueOf(a).length(); // Count number of digits

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

Explaination:
import java.util.Scanner;

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(System.in);


System.out.print("Enter a number: ");
int num = sc.nextInt();

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

int numDigits = String.valueOf(num).length();


System.out.println("Number of digits: " + numDigits);

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

sc.close();

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 java.util.Scanner;

public class StrongNUmberOrNot {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt(); // Read user input

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


boolean isStrong = obj.isStrong(n); // Call method to check Strong Number

if (isStrong) {
System.out.println(n + " is a Strong Number.");
} else {
System.out.println(n + " is not a Strong Number.");
}

sc.close(); // 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 java.util.Scanner;

public class FriendlyPairs {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();

boolean isFriendly = FriendlyPairsUtility.isFriendlyPair(num1, num2);

if (isFriendly) {
System.out.println(num1 + " and " + num2 + " are friendly pairs.");
} else {
System.out.println(num1 + " and " + num2 + " are not friendly pairs.");
}

sc.close();
}
}

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 <= Math.sqrt(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. FriendlyPairs.java (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. FriendlyPairsUtility.java (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 java.util.Scanner;

public class AbundantNumberCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = sc.nextInt();

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


if (AbundantNumberUtility.isAbundant(num)) {
System.out.println(num + " is an abundant number.");
} else {
System.out.println(num + " is not an abundant number.");
}

sc.close();
}
}

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. AbundantNumberCheck.java (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. AbundantNumberUtility.java (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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy