The Xylem and Phloem numbers in Java
To determine if a particular number is a xylem or phloem, we will understand what xylem and phloem numbers are in this section. The xylem and phloem number are usually asked in Java coding exams and assignments.
If the sum of a number N's extreme (first and last) digits equals the sum of its mean (all digits except first and last), then that number is a xylem number. The number is referred to as a phloem number if the sum of the extreme digits does not match the sum of the mean digits.
Xylem numbers
The sum of extreme digits = Sum of mean digits
Examples –
- 11337
X = The sum of extreme numbers 1+7 = 8
Y = The sum of mean numbers 1 + 3 + 3 = 8
X is equal to Y - 7163429
X = The sum of extreme numbers 7 + 9 = 16
Y = The sum of mean numbers 1 + 6 + 3 + 4 + 2 + 9 = 16
X is equal to Y
Phloem numbers
The sum of extreme digits ≠ Sum of mean digits
Examples –
- 27565
X = The sum of extreme numbers 2 + 5 = 7
Y = The sum of mean numbers 7 + 5 + 6 = 18
X is not equal to Y - 45469
X = The sum of extreme numbers 4 + 9 = 13
Y = The sum of mean numbers 5 + 4 + 6 = 15
X is not equal to Y
Algorithm:
We will use the below steps to check if a number is a xylem or phloem.
Step 1. Import the necessary packages, java.util.* and java.io.*, to use the Scanner class and input/output functions.
Step 2. Define the class "XylemPhloem" with the main method.
Step 3. Initialize the variables "extremeD_sum" and "meanD_sum" to store the sum of the extreme digits and mean digits of the number, respectively.
Step 4. Use the Scanner class to read an integer input from the user.
Step 5. Find the absolute value of the given number and store it in the same variable.
Step 6. Store the value of the input number in another variable, num1.
Step 7. Use a while loop to repeatedly find the last digit of the number stored in num1 until the number becomes zero.
Step 8. If the condition in the if statement is true (i.e., num1 is either equal to the original number or less than 10), the last digit is added to the extremeD_sum. If the condition is false, the last digit is added to meanD_sum.
Step 9. Divide num1 by 10 to remove the last digit.
Step 10. Repeat steps 7 to 9 until the number stored in num1 becomes zero.
Step 11. Print the sum of extreme digits and the sum of mean digits.
Step 12. Compare the sum of extreme digits with the sum of mean digits.
Step 13. If they are equal, print that the number is a "xylem number", otherwise, print that the number is a "phloem number".
Top of Form
let’s implement the above steps in Java code to check for xylem and phloem number.
Implementation 1:
File name - XylemPhloem. java
import java.util.*;
import java.io.*;
public class XylemPhloem
{
public static void main(String args[])
{
// the sum of extreme digits is stored in the variable extreme_sum
// the sum of mean digits is stored in the variable mean_sum
int num, extremeD_sum = 0, meanD_sum = 0, num1;
Scanner sc= new Scanner (System.in);
System.out.print("Enter any number: ");
//reads an integer from the user
num = sc.nextInt();
// the absolute value of the given number is found out
num = Math.abs(num);
//the entered number is copied into variable num1
num1 = num;
//the while loop executes until the given condition becomes false
while(num1 != 0)
{
//If one of the conditions is true, the statement is considered true
if(num1 == num || num1 < 10)
//The last digit is found out and added to the variable extreme_sum
extremeD_sum = extremeD_sum + num1 % 10;
else
//The sum of mean digits is found and added to the variable mean_sum
meanD_sum = meanD_sum + num1 % 10;
//The last digit from the number is removed
num1 = num1 / 10;
}
System.out.println("The sum of extreme digits are " + extremeD_sum );
System.out.println("The sum of mean digits are " + meanD_sum);
// The sum of extreme digits is compared with the sum of mean digits
if(extremeD_sum == meanD_sum)
//displays if sums are equal
System.out.println(num + " is a xylem number.");
else
//displays if the sum is not equal
System.out.println(num + " is a phloem number.");
}
}
Output 1:

Output 2:

Complexity Analysis:
The time complexity of the above program can be analyzed as follows:
The while loop that calculates the sum of the digits of the number runs for a maximum of log10(n) times, where n is the number of digits in the input number.
Therefore, the overall time complexity of the program is O(log10(n)), which is linear in the number of digits in the input number.
The space complexity of the program is O(1), as only a few variables are used and their size does not depend on the input size.