Jump to content

Java Help - Range calculation


nagchiru

Recommended Posts

Hello experts, I need to check if a particular range of numbers falls in a given range of numbers. For example I need to check if 10 to 20 falls in range of 1 to 100, the program should return "inRange" in this case, if the ranges are overlapping the program needs to indicated the same ex: 50 to 150 overlaps with 1 to 100 range.

Please help me to write this java program.

Link to comment
Share on other sites

int intiailValue=10,finalValue=50 or try some other value;



for(i=intialValue;i<=finalValue;i++)
{
if(i>100){
s.o.p("RESULT:"+intialvalue+"to"+(i-1)+"are in  the range and from "+i+"to "+finalValue+" are out of range");
break;
}

}
if(finalValue<=100){
sop("in range")

this is a small idea u can improvize it

Link to comment
Share on other sites

public class InRange {

public static void main(String args[]){

int min = 1;
int max = 100;

int checkMin = 10;
int checkMax = 20;

if((checkMin < min && checkMax < min) || (checkMin > max && checkMax > max)){
System.out.println("Out of Range");
}

if(checkMin < min && checkMax > min){
System.out.println("Overlapping range");
int overlap = checkMax - min;
System.out.println("Number of numbers overlapping "+overlap);
System.out.println("Overlapping range :" +min+ " - "+checkMax);
}

if(checkMin > min && checkMax < max){
System.out.println("InRange");
}

if(checkMin > min && checkMax > max){
System.out.println("Overlapping range");

int overlap = max - checkMin;
System.out.println("Number of numbers overlapping "+overlap);
System.out.println("Overlapping range :" +checkMin+ " - "+max);
}

if(checkMin < min && checkMax > max){
System.out.println("Overlapping range");

int overlap = max - min;
System.out.println("Number of numbers overlapping "+overlap);
System.out.println("Overlapping range :" +min+ " - "+max);
}
}

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...