Skip to content

Instantly share code, notes, and snippets.

@laim2003
Last active October 29, 2018 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laim2003/824024a07f54b19ddfd7e4bbfacfe3db to your computer and use it in GitHub Desktop.
Save laim2003/824024a07f54b19ddfd7e4bbfacfe3db to your computer and use it in GitHub Desktop.
package com.my.package;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
private static int[] nums = {1,4,5,7,8,9};//Some random SORTED array
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("array ="+ Arrays.toString(nums));
System.out.println("Give me a number:");
int num = sc.nextInt();
int posInArray = 0;
boolean existsInArray;
for(int i = 0;i < nums.length;i++){
existsInArray = num == nums[i];
posInArray = num > nums[i]?i+1:posInArray;
if(existsInArray){
posInArray = i;
System.out.println("Element exists in array");
break;
}
}
System.out.println("Position in array is/would be "+posInArray);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment