Xylem and Phloem Number in Java

In this section, we will learn what is xylem and phloem number and also create Java program to check if the given number is xylem or phloem. The xylem and phloem number program frequently asked in Java coding tests and academics.

Important Interview Questions answer

More Course

Free Courses Enroll Today

Xylem and Phloem Number

A number N will be a xylem number if the sum of its extreme (first and last) digits is equal to the sum of mean (all digits except first and last) digits. If the sum of extreme digits is not equal to the sum of mean digits, the number is called phloem number.

Xylem and Phloem Number in Java

In short, we can say that:

Xylem NumberSum of extreme digits = Sum of mean digits

Xylem NumberSum of extreme digits ≠ Sum of mean digits

Xylem and Phloem Number Example

Example of Xylem and Phloem Numbers
NumberExtreme DigitsSum of Extreme DigitsMean DigitsSum of Mean DigitsComparing SumXylem or Phloem
123481, 892, 3, 499 = 9Xylem
122251, 562, 2, 266 = 6Xylem
8251228, 2102, 5, 1, 21010 = 10Xylem
7613127, 296, 1, 3, 1119 ≠ 11Phloem
2713892, 9117, 1, 3, 81911 ≠ 19Phloem
171561, 677, 1, 5137 ≠ 13Phloem

Steps to find Xylem and Phloem Number

  1. Read or initialize a number N.
  2. Find the extreme digits of N.
  3. Sum up the extreme digits and store the sum in a variable (extreme_sum).
  4. Find the mean digits of N.
  5. Sum up the mean and store the sum in a variable (mean_sum).
  6. Compare both sums (that we get from steps 3 and 5).
    • If they are equal, the number is a xylem
    • Else, the number is a phloem

Let’s implement the above steps in a Java program and check if the given number is xylem or phloem.

Xylem and Phloem Number Java Program

XylemPhloemExample .java

import java.util.*;  
import java.io.*;  
public class XylemPhloemExample  
{  
public static void main(String args[])  
{  
//the variable extreme_sum stores the sum of extreme digits   
//the variable mean_sum stores the sum of mean digits   
int num, extreme_sum = 0, mean_sum = 0, n;   
Scanner sc= new Scanner (System.in);  
System.out.print("Enter a number: ");  
//reading an integer from the user  
num = sc.nextInt();  
//finds the absolute value of the given number  
num = Math.abs(num);  
//copying the given number into n  
n = num;  
//the while loop executes until the specified condition becomes false  
while(n != 0)  
{  
//returns true if one of the conditions is true  
if(n == num || n < 10)  
//finds the last digit and add it to the variable extreme_sum  
extreme_sum = extreme_sum + n % 10;  
else  
//finds the mean digits and add it to the variable mean_sum  
mean_sum = mean_sum + n % 10;  
//removes the last digit from the number  
n = n / 10;  
}  
System.out.println("The sum of extreme digits: " + extreme_sum );  
System.out.println("The sum of mean digits: " + mean_sum);  
//comparing the sum of extreme digits and with the sum of mean digits  
if(extreme_sum  == mean_sum)  
//prints if sums are equal  
System.out.println(num + " is a xylem number.");  
else  
//prints if sums are not equal  
System.out.println(num + " is a phloem number.");  
}  
}  

Output 1:

Enter a number: 825122
The sum of extreme digits: 10
The sum of mean digits: 10
825122 is a xylem number.

Output 2:

Enter a number: 761312
The sum of extreme digits: 9
The sum of mean digits: 11
761312 is a phloem number.

Important Interview Questions answer

Leave a Comment