Guest User

Increasing / Decreasing / Bouncy Number

a guest
Oct 29th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. /**
  2.  * This program inputs a number and checks whether the number entered is an increasing number, a decreasing number or a bouncy number.
  3.  * For more information, please visit: https://goo.gl/L5YfXo
  4.  */
  5. import java.util.Scanner;
  6. public class Bouncy_Number
  7. {
  8.     static boolean isIncreasing(int num)
  9.     {
  10.         String a = Integer.toString(num);
  11.         int i,x,y,flag=1;
  12.         for(i=1;i<a.length();i++)
  13.         {
  14.             x=Character.getNumericValue(a.charAt(i));
  15.             y=Character.getNumericValue(a.charAt(i-1));
  16.             if(x < y)
  17.             {
  18.                 flag = 0;
  19.                 break;
  20.             }
  21.         }
  22.        
  23.         if(flag == 1)
  24.             return true;
  25.         else
  26.             return false;
  27.     }
  28.    
  29.     static boolean isDecreasing(int num)
  30.     {
  31.         String a = Integer.toString(num);
  32.         int i,x,y,flag=1;
  33.         for(i=0;i<a.length()-1;i++)
  34.         {
  35.             x=Character.getNumericValue(a.charAt(i));
  36.             y=Character.getNumericValue(a.charAt(i+1));
  37.             if(x < y)
  38.             {
  39.                 flag = 0;
  40.                 break;
  41.             }
  42.         }
  43.        
  44.         if(flag == 1)
  45.             return true;
  46.         else
  47.             return false;
  48.     }
  49.    
  50.     public static void main(String[] args)
  51.     {
  52.         Scanner sc = new Scanner(System.in);
  53.         System.out.println("Please enter a number.");
  54.         int num = sc.nextInt();
  55.         if(num >= 100) //Since there can't be any bouncy numbers lesser than 100.
  56.         {
  57.             boolean check1 = isIncreasing(num);
  58.             boolean check2 = isDecreasing(num);
  59.             if(check1 && check2 == false) //Only increasing
  60.                 System.out.println(num+" is increasing and not a bouncy number");
  61.             else if(check1 == false && check2) //Only decreasing
  62.                 System.out.println(num+" is decreasing and not a bouncy number.");
  63.             else
  64.                 System.out.println(num+" is a bouncy number.");
  65.         }
  66.         else
  67.             System.out.println("Incorrect input. Please enter a number exceeding 100.");
  68.        
  69.         sc.close();
  70.     }
  71. }
Add Comment
Please, Sign In to add comment