write a java program of friendly number
A friendly number is a positive integer that is equal to the sum of its proper divisors (excluding itself). In other words, a friendly number is a number that is the sum of its factors, except for the number itself.
java programming beginner guide
For example, the number 6 is a friendly number because its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6. Another example is the number 28, which has proper divisors 1, 2, 4, 7, and 14, and 1 + 2 + 4 + 7 + 14 = 28.
Friendly numbers are a type of integer sequence that have been studied in mathematics for centuries, and they have many interesting properties and applications in various fields, including number theory, cryptography, and computer science.
import java.util.Scanner;
public class FriendlyNumber {
public static void main(String[] args) {
// Create a scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Read the input number from the user
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Calculate the sum of the divisors of the input number
int sum = 0;
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i;
}
}
// Determine the friendly number based on the sum of divisors
String friendlyNumber;
if (sum == number) {
friendlyNumber = "a perfect number";
} else if (sum < number) {
friendlyNumber = "a deficient number";
} else {
friendlyNumber = "an abundant number";
}
// Output the friendly number
System.out.println("The number " + number + " is " + friendlyNumber + ".");
}
}
Comments
Post a Comment