Xylem and Phloem Number in Java

In this section, we will explore the concepts of xylem and phloem numbers and develop Java programs to determine whether a given number qualifies as a xylem or phloem number. These programs are commonly encountered in Java coding tests and academic assignments. Our focus will be on understanding the characteristics of xylem and phloem numbers and implementing code to check if a given number satisfies the criteria for being classified as a xylem or phloem number.

What is a Xylem and Phloem Number

A number is classified as a xylem number if the sum of its first and last digits is equal to the sum of its middle digits (all digits except the first and last). On the other hand, if the sum of the first and last digits is not equal to the sum of the middle digits, the number is referred to as a phloem number. In our exploration, we will understand the properties of xylem and phloem numbers and create Java programs to determine whether a given number qualifies as a xylem or phloem number. These programs are commonly encountered in Java coding tests and academic assignments.

Xylem Number:

Sum of extreme digits = Sum of mean digits

Xylem Number

Sum of extreme digits ≠ Sum of mean digits

Xylem and Phloem Number Example

Number Extreme Digits The sum of Extreme Digits Mean Digits The sum of Mean Digits Comparing Sum Xylem or Phloem
12348 1, 8 9 2, 3, 4 9 9 = 9 Xylem
12225 1, 5 6 2, 2, 2 6 6 = 6 Xylem
825122 8, 2 10 2, 5, 1, 2 10 10 = 10 Xylem
761312 7, 2 9 6, 1, 3, 1 11 9 ≠ 11 Phloem
271389 2, 9 11 7, 1, 3, 8 19 11 ≠ 19 Phloem
17156 1, 6 7 7, 1, 5 13 7 ≠ 13 Phloem

Steps to find Xylem and Phloem Number

  1. Read or initialize a number N.
  2. Find the extreme digits of N (the first and last digits).
  3. Sum up the extreme digits and store the sum in a variable (extreme_sum).
  4. Find the mean digits of N (all digits except the first and last).
  5. Sum up the mean digits and store the sum in a variable (mean_sum).
  6. Compare the sums obtained in steps 3 and 5.
  7. If the sums are equal, the number is classified as a xylem number.
  8. If the sums are not equal, the number is classified as a phloem number.

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.

If you want to explore topics like Xylem and Phloem Number in Java in greater detail, we encourage you to visit tutorials.freshersnow.com for additional insights and knowledge.