KNA-9 Logic_Building_Problems
KNA-9 Logic_Building_Problems
9.Assignment Submission
Student Name: Praveen Biradar
ID: KODD8KS8I
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:
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;
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.
This defines the main class and method where the program starts execution.
This creates a Scanner object, prompts the user to enter a number, and stores it in num.
This converts num to a string, counts the number of digits, and prints the result.
sc.close();
A Strong Number is a number where the sum of the factorials of its digits is equal to the original number.
Example:
1. 145
package logicBuildingExamplesLevel_2;
import java.util.Scanner;
if (isStrong) {
System.out.println(n + " is a Strong Number.");
} else {
System.out.println(n + " is not a Strong Number.");
}
package logicBuildingExamplesLevel_2;
while (n > 0) {
int digit = n % 10; // Extract last digit
sum += factorial(digit); // Compute factorial and add to sum
n /= 10; // Remove last digit
}
Explanation of StrongNUmberOrNot1
Method: isStrong(int n)
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
package logicBuildingExamplesLevel_2;
import java.util.Scanner;
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;
How It Works
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;
sc.close();
}
}
package logicBuildingExamplesLevel_2;
Compile both files (ensuring they are in the same package/directory) and run the main class to check for
abundant numbers.