Question:
Write a Program in Java to input a number and check whether it is a Bouncy Number or not.
Increasing Number : Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 22344.
Decreasing Number : Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 774410.
Bouncy Number : We shall call a positive integer that is neither increasing nor decreasing a “bouncy” number; for example, 155349. Clearly there cannot be any bouncy numbers below 100.
Solution:
/** * The class BouncyNumber inputs a number and checks whether it is a Bouncy Number or not * @author : www.guideforschool.com * @Program Type : BlueJ Program - Java */ import java.util.*; class BouncyNumber { boolean isIncreasing(int n) //Function to check whether a number is Increasing { String s = Integer.toString(n); char ch; int f = 0; for(int i=0; is.charAt(i+1))// If any digit is more than next digit then we have to stop checking { f = 1; break; } } if(f==1) return false; else return true; } boolean isDecreasing(int n) //Function to check whether a number is Decreasing { String s = Integer.toString(n); char ch; int f = 0; for(int i=0; i
Output:
Enter a number : 22344
The number 22344 is Increasing and Not Bouncy
Enter a number : 774410
The number 774410 is Decreasing and Not Bouncy
Enter a number : 155349
The number 155349 is bouncy
155349
1<5
isIncreasing will return true…right?
No, as soon as it gets any digit more than earlier digit, it will return false. If all digits are less than next digit, then only it will return true.
Will I get full marks if i perform string operations on number programs like kaprekar number or keith number?
yes