Showing posts with label Xylem And Phloem Number in Java. Show all posts
Showing posts with label Xylem And Phloem Number in Java. Show all posts

Xylem And Phloem Number 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."); 
 }
 }

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...