Find number is present in Array or Not
Find number is present in Array or Not
- // Java code import java.util.*;
class A {
static boolean exists(int[] ints, int k) { boolean found=false; for(int n:ints){ if(n==k){ found=true; break; } } return found; } public static void main(String[] args){
int[] ints={-9,14,37,102}; System.out.print("Enter num:"); Scanner s=new Scanner(System.in); int k=s.nextInt(); System.out.println("Number exist:"+exists(ints,k)); }}
// Java code
import java.util.*;
class A {
static boolean exists(int[] ints, int k) {
boolean found=false;
for(int n:ints){
if(n==k){
found=true;
break;
}
}
return found;
}
public static void main(String[] args){
int[] ints={-9,14,37,102};
System.out.print("Enter num:");
Scanner s=new Scanner(System.in);
int k=s.nextInt();
System.out.println("Number exist:"+exists(ints,k));
}
}
Comments
Post a Comment