Last active
October 29, 2018 18:59
-
-
Save laim2003/824024a07f54b19ddfd7e4bbfacfe3db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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