In this program, a number will be entered by user and the program will show the minimum digit in the number. If the number does not contain any prime digit, it will display a message.
How to proceed on this program
A variable ‘min’ is used in this program to hold the minimum prime digit present in the number. The initial value of ‘min’ is set to -1, as the minimum prime digit can not be negative. From the entered number, the digits are extracted using a while loop and checked for prime. If any digit is found prime, the first such digit is assigned on ‘min’ and if there are more prime digits, they are checked against ‘min’ and appropriate action is taken. At the end of loop is the value in ‘min’ is found -1 it means there is no prime digit in the entered number otherwise the variable ‘min’ holds the minimum prime digit.
import java.io.*;
class Find
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i, p,min=-1,n;
public void take()throws Exception
{
System.out.println("Enter the number :");
n=Integer.parseInt(br.readLine());
while(n >0)
{
p=n%10;
if(prime(p))
{
if(min==-1)
min=p;
else if(p< min)
min=p;
}
n=n/10;
}
if(min==-1)
System.out.println("No prime digit in the number:");
else
System.out.println("Minimum prime digit="+min);
}
private boolean prime(int n)
{
int i;
for(i=2;i< n;i++)
{
if(n%i==0)
break;
}
if(i==n)
return true;
else
return false;
}
}
Related Post: BlueJ Programs on Number
No comments:
Post a Comment