Java Program to Check Xylem and Phloem Number

In the previous article, we have discussed Java Program to Check Strontio Number

In this article we are going to understand what Xylem and Phloem number in java is and how we can check whether a number is Xylem or Phloem or not in Java with examples. And What xylem and phloem program in java, Xylem number in java, xylem program in java, Xylem number program in java, xylem and phloem number examples, smith number etc..

Also Read: Java Program to Check Smith Number

Program to Check Xylem and Phloem Number

Xylem numbers are numbers whose sum of extreme digits is equal to the sum of its mean digits, else it is called a Phloem Number. Extreme digits are the first and last digit and mean digits are all the digits except those two.

 Example :

34326    : 3+6 = 9;  4+3+2= 9   Xylem number
173156  : 1+6 = 7;  7+3+1=11  Phloem number

In the above examples the number 34326 is a Xylem number as the sum of its mean and extreme digits are same, while 173156 is a Phloem number as the sum is different.

Let’s see different ways to check Xylem and Phloem number.

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach :

  1. Enter/declare a number and store it .
  2. Then we add the extreme digits of the number.
  3. Next we add the mean digits of the number.
  4. If both the sum are equal, then the number is said to be a Xylem number else it is a Phloem number.

Method-1: Java Program to Check Xylem and Phloem Number By Using Static Value

import java.util.Scanner;
public class XylemPhloemNumber{
    public static void main(String args[])
    {
        //A number declared
        int num = 34326;

    int temp = num, extremeSum=0, meanSum=0;
    
    while(temp != 0)  
    {
        if(temp == num || temp < 10)  
            //Adds the first and last digits
            extremeSum = extremeSum + temp % 10;  
        else  
            //finds the mean digits and adds  
            meanSum = meanSum + temp % 10;
        temp = temp / 10;  
    }

    if(extremeSum==meanSum)
    {
        System.out.println(num+" is a Xylem number");
    }
    else
    {
        System.out.println(num+" is a Phloem number");
    }
    }
}
Output:

34326 is a Xylem number

Method-2: Java Program to Check Xylem and Phloem Number By User Input Value

import java.util.Scanner;
public class XylemPhloemNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

    int temp = num, extremeSum=0, meanSum=0;
    
    while(temp != 0)  
    {
        if(temp == num || temp < 10)  
            //Adds the first and last digits
            extremeSum = extremeSum + temp % 10;  
        else  
            //finds the mean digits and adds  
            meanSum = meanSum + temp % 10;
        temp = temp / 10;  
    }

    if(extremeSum==meanSum)
    {
        System.out.println(num+" is a Xylem number");
    }
    else
    {
        System.out.println(num+" is a Phloem number");
    }
    }
}
Output:

Case-1

Enter a number : 34326 
34326 is a Xylem number

Case-2

Enter a number : 1551 
1551 is a Phloem number

Method-3: Java Program to Check Xylem and Phloem Number By User Defined Method

import java.util.Scanner;
public class XylemPhloemNumber{
    public static void main(String args[])
    {
        //A number declared
        int num = 34326;
        
        //user defined method check() method called
        check(num);
    }
    
    
    //check() method to check  Xylem or Phloem Number
    public static void check(int num)
    {
        int temp = num, extremeSum=0, meanSum=0;
        while(temp != 0)  
        {
            if(temp == num || temp < 10)  
                //Adds the first and last digits
                extremeSum = extremeSum + temp % 10;  
            else  
                //finds the mean digits and adds  
                meanSum = meanSum + temp % 10;
            temp = temp / 10;  
        }
    
        if(extremeSum==meanSum)
        {
            System.out.println(num+" is a Xylem number");
        }
        else
        {
            System.out.println(num+" is a Phloem number");
        }
    }
}
Output:

34326 is a Xylem number

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Try these: 

  1. Write A Java Program To Print The Number Is Xylem Or Phloem?
  2. What Is Xylem Number?
  3. What Is Xylem And Phloem Number?

Related Java Programs: