You are on page 1of 2

Class climbing_number 1/2

/*
* ashking13th@gmail.com
* javaprogramsbyashking13th.blogspot.in
* ashkingjava.blogspot.in
*
* QUESTION
*
* Design a program in java to check whether a number
* is climbing number or not.Also print the digits of
* the number.
* A climbing number is a number whose digits are in
* ascending order.
* eg. 123 ,159 ,126 ,etc.
*/
import java.io.*;
public class climbing_number
{
public void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the number");
int n=Integer.parseInt(d.readLine());
int c=0;
//counter to store number of digits of the number
int a=n;
int z=n;
while(a>0)//loop to count no of digits
{
c=c+1;
//updating counter variable to count no. of digits
a=a/10;
}
int m[]=new int[c];
int y=0;
for(int i=0;i<c;i++)
{
y=z%10; //extracting digits
m[i]=y; //storing digits of the number in an array
z=z/10; //updating loop variable
}
for(int w=0;w<c;w++)
{
System.out.print(m[c-1-w]+"\t");
//printing the digits of the number
}
int p=0;
for(int e=0;e<(c-1);e++)
{
if(m[e]>m[e+1])
//comparing digits of the number
p=p+1;
Mar 25, 2014 3:08:04 PM
Class climbing_number (continued) 2/2
//counting the no. of times where a digit is smaller than
//the next digit.(array is storing the digits fom last to first)
/*
* we compare every digit eith the next digit.
* If every digit is smaller than the next digit
* then the number must be climbing.
* So we count the number of times the required condition
* is true for the number .If it is 1 less than the total
* number of digits then the number must be clmbing because
* we have to compare the digits c-1 times .
* So if every time it is true then our condition is fullfiled.
*/
}
if(p==(c-1))//checking if digits are in increasing order
{
System.out.println("climbing number");
}
else
{
System.out.println("not a climbing number");
}
}//end of main method
}//end of class
Mar 25, 2014 3:08:04 PM

You might also like