Xylem and Phloem Number in Java



A number is a Xylem number if the sum of extreme digits (first and last) equals the sum of its mean digits (all digits except first and last). If the sum of the extreme digits does not match the sum of the mean digits, then it is a Phloem number.

We can understand it with the help of examples ?

Example 1

Take the number 12326:
Sum of its extremes is 1+6=7.
The sum of its mean digits is 2+3+2=7.
Sum of extremes = Sum of mean digits, so it is a xylem number.

Example 2

The number is 36985.
The sum of its extremes is 3+5=8.
The sum of its mean digits is 6+9+8=23.
The sum of extremes is not equal to the sum of its mean digits, so it is a Phloem number.

Algorithm 1: Using a while loop

In this approach, we take a number as input and check if it is a Xylem or Phloem number. We use a while loop here to iterate through the number and also use the modulus operator to find the last digit of the number.

Implementation steps

Step 1 ? Input the number to check for xylem and phloem using the Scanner class.

Step 2 ? Initialize s1 for calculating the sum of extremes and s2 for calculating the sum of the mean as 0. Also create a duplicate number, which is a copy of the original number.

Step 3 ? First, initialize the last digit of the number to s1 and remove the last digit.

Step 4 ? Then create a while loop that runs till we reach the first digit to add all the mean digits.

Step 5 ? After the loop, add the first digit to s1.

Step 6 ? Now check if s1 is equal to s2. If yes, print the number is Xylem number; otherwise, print the number is Phloem number.

Implementation code

The following Java program implements the while loop approach to determine whether a number is a Xylem or Phloem number.

import java.util.*;
class XylemPhloemNumber {      
   public static void main(String []args) {
      Scanner sc =new Scanner(System.in);
      System.out.print("Enter a number:");
      int n=sc.nextInt();
      int s1=0;
      int s2=0;
      int d=n;
      s1=n%10;
      n=n/10;
      while(n>9) {
         s2=s2+n%10;
         n=n/10;
      }
      s1=s1+n;
      if(s1==s2)
         System.out.println(d+" is a Xylem Number");
      else
         System.out.println(d+" is a Phloem Number");
   }
}

Output

Enter a number: 64312
64312 is a Xylem Number
Enter a number: 236451
236451 is a Phloem Number

Time complexity

The time complexity of this code will be O(n) where n is the number of digits of the number.

Approach 2: using a string operator

We can also write the same program using string operators. Instead of using mathematical operations like ?% 10' or ?/ 10' we use string operators to get the digit at a particular position.

Implementation steps

Step 1 ? Input the number to check for xylem and phloem using the Scanner class.

Step 2 ? Initialize s1 for calculating the sum of extremes and s2 for calculating the sum of the mean as 0.

Step 3 ? First, convert the number to a string value.

Step 4 ? Using charAt() function get the first and last digits and add them to s1.

Step 5 ? Get the digits between the first and last digit using a simple for loop and assign their sum to s2.

Step 6 ? Now check if s1 and s2 are equal or not. If equal, print the number is Xylem number; otherwise, print the number is Phloem number.

Implementation code

This Java program demonstrates how to check for Xylem and Phloem numbers using string operations instead of mathematical calculations.

import java.util.*;

class XylemPhloemNumber {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter a number: ");
      int n = sc.nextInt();
      String numStr = String.valueOf(Math.abs(n));

      int s1 = 0, s2 = 0;
      s1 += Integer.parseInt(String.valueOf(numStr.charAt(0)));
      s1 += Integer.parseInt(String.valueOf(numStr.charAt(numStr.length() - 1)));

      for (int i = 1; i < numStr.length() - 1; i++) {
         s2 += Integer.parseInt(String.valueOf(numStr.charAt(i)));
      }

      if (s1 == s2) {
         System.out.println(n + " is a Xylem Number");
      } else {
         System.out.println(n + " is a Phloem Number");
      }
   }
}

Output

Enter a number: 5322338
5322338is a Xylem Number
Enter a number: 2765483
2765483 is a Phloem Number

Time complexity

The time complexity of this code will be O(n) where n is the number of digits of the number.

Updated on: 2025-02-28T14:28:54+05:30

403 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements