Categories
Class 10 Class 9

Xylem and Phloem Numbers in Java

A xylem number is a number such that the sum of extreme digits equals the sum of the mean digits. Otherwise the number is considered as the phloem number.

Write a program in Java to accept a positive integer from the user. Check whether it is a xylem number or a phloem number.

Example:
INPUT:
N = 12348
OUTPUT:
Sum of extreme digits = 1 + 8 = 9.
Sum of mean digits = 2 + 3 + 4 = 9.
Thus, 12348 is a xylem number.

import java.io.*;
class Xylem{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("N = ");
        int num = Math.abs(Integer.parseInt(br.readLine()));
        int extreme = 0;
        int mean = 0;
        int copy = num;
        while(copy != 0){
            if(copy == num || copy < 10)
                extreme += copy % 10;
            else
                mean += copy % 10;
            copy /= 10;
        }
        System.out.println("Sum of extreme digits: " + extreme);
        System.out.println("Sum of mean digits: " + mean);
        if(extreme == mean)
            System.out.println(num + " is a xylem number.");
        else
            System.out.println(num + " is a phloem number.");
    }
}

By admin

I am a Computer Science teacher in one of the renowned schools in India. I teach Java programming language to my students, and I maintain a website happycompiler.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.