Sie sind auf Seite 1von 3

Class climbing_prime 1/3

/*
* ashking13th@gmail.com
* javaprogramsbyashking13th.blogspot.in
* ashkingjava.blogspot.in
*
*
* QUESTION
*
* Design a program in java to check whether a number is a
* climbing prime or not.
* A climbing prime is a number which is prime and also a
* climbing number(whose digits are in ascending order)
* eg, 137
*/
import java.io.*;
public class climbing_prime
{
boolean prime(int n)
{
int v=0;
for(int g=1;g<=n;g++)
{
if(n%g==0)
{
v=v+1;
}
}
if(v==2)
{
return true;
}
else
{
return false;
}
}
public boolean climbing(int n)
{
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;
Mar 25, 2014 3:12:37 PM
Class climbing_prime (continued) 2/3
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
}
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;
//counting the no. of times where a digit is smaller than
//the next digit.(array is storing the digits fom last to firs
t)
/*
* 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
{
return true;
}
else
{
return false;
}
}
//end of main method
public void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the number");
int n=Integer.parseInt(d.readLine());
climbing_prime obj1=new climbing_prime();
/*
* If the number is climbing and prime both simultaneously
* then only is the number a climbing prime.
Mar 25, 2014 3:12:37 PM
Class climbing_prime (continued) 3/3
*/
if(climbing(n))
{
if(prime(n))
{
System.out.println("its a climbing prime");
}
else
{
System.out.println("its not a climbing prime");
}
}
else
{
System.out.println("its not a climbing prime");
}
}
//end of main
}
//end of class
Mar 25, 2014 3:12:37 PM

Das könnte Ihnen auch gefallen