Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This program inputs a number and checks whether the number entered is an increasing number, a decreasing number or a bouncy number.
- * For more information, please visit: https://goo.gl/L5YfXo
- */
- import java.util.Scanner;
- public class Bouncy_Number
- {
- static boolean isIncreasing(int num)
- {
- String a = Integer.toString(num);
- int i,x,y,flag=1;
- for(i=1;i<a.length();i++)
- {
- x=Character.getNumericValue(a.charAt(i));
- y=Character.getNumericValue(a.charAt(i-1));
- if(x < y)
- {
- flag = 0;
- break;
- }
- }
- if(flag == 1)
- return true;
- else
- return false;
- }
- static boolean isDecreasing(int num)
- {
- String a = Integer.toString(num);
- int i,x,y,flag=1;
- for(i=0;i<a.length()-1;i++)
- {
- x=Character.getNumericValue(a.charAt(i));
- y=Character.getNumericValue(a.charAt(i+1));
- if(x < y)
- {
- flag = 0;
- break;
- }
- }
- if(flag == 1)
- return true;
- else
- return false;
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Please enter a number.");
- int num = sc.nextInt();
- if(num >= 100) //Since there can't be any bouncy numbers lesser than 100.
- {
- boolean check1 = isIncreasing(num);
- boolean check2 = isDecreasing(num);
- if(check1 && check2 == false) //Only increasing
- System.out.println(num+" is increasing and not a bouncy number");
- else if(check1 == false && check2) //Only decreasing
- System.out.println(num+" is decreasing and not a bouncy number.");
- else
- System.out.println(num+" is a bouncy number.");
- }
- else
- System.out.println("Incorrect input. Please enter a number exceeding 100.");
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment