This is a java program, user will enter the lower and upper limit and program will show the longest gap between two non prime numbers along with the starting and ending non prime numbers.
import java.util.*;
class Ab
{
int i,n,m,max_non_prime,max=0,gap=0;
Scanner sc=new Scanner(System.in);
void show()
{
System.out.print("Enter Lower Ranger:");
n=sc.nextInt();
System.out.print("Enter Upper Ranger:");
m=sc.nextInt();
System.out.print("\nPrime Numbers are:");
for(i=n;i<=m;i++)
{
if(!prime(i))
gap++;
else
{
System.out.print(i+ " ");
if(gap>max)
{
max=gap;
max_non_prime=i-1;
}
gap=0;
}
}
System.out.println("\nMaximum Gap="+max);
for(i=max_non_prime;i>=n;i--)
{
if (prime(i))
break;
}
System.out.print("Starting Non Prime: "+(i+1) + " End Non Prime: "+max_non_prime);
}
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;
}
public static void main(String args[])
{
Ab object=new Ab ();
object.show();
}
}
Other Programs On Numbers: CLICK HERE
No comments:
Post a Comment