AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Java program to get n integers from the user and a number to be searched

Write a java program to get n integers from the user and a number to be searched.

Your program should also print the number of integers which are less than the search number and greater than the search number.
Input specification:

Number of elements in the array, the N elements and the number to be searched.

Output specification:

Print "YES" or "NO" if the search number is present in the array,

the number of elements which are less than the search number and

the number of elements which are greater than the search number

corejava x 348
programming x 165
Posted On : 2017-09-13 23:03:22.0
profile isha srivastava - anyforum.in isha srivastava
700
up-rate
7
down-rate

Answers


import java.util.Scanner;

public class Search {
public static void main(String[] args) {
System.out.println("Please Enter The Total Number of Elements");
try{
Scanner input=new Scanner(System.in);
int N=input.nextInt();
int[] elements=new int[N];
System.out.println("Please Enter the elements one by one");
for(int i=0;i<N;i++){
elements[i]=input.nextInt();
}
System.out.println("Please Enter the element to be searched");
int find=input.nextInt();
boolean isFound=false;
int lessCount=0;
int highCount=0;
for(int n:elements){
if(n==find)
isFound=true;
else if(n<find)
lessCount++;
else
highCount++;
}
System.out.println(isFound?"Yes":"No");
System.out.println(lessCount);
System.out.println(highCount);
}catch (Exception e) {
e.printStackTrace();
}
}
}

Posted On : 2017-09-14 22:28:57
Satisfied : 5 Yes  4 No
profile Garima Gupta - anyforum.in Garima Gupta
596128524362
Reply This Thread
up-rate
0
down-rate
Comments
It is saying:
Compilation Error:
---------------------------------------------------------
Line:/Search.java:18: error: cannot find symbol
boolean isFound=False;
^
symbol: variable False
location: class Search
profile isha srivastava - anyforum.in isha srivastava
7  0  0
Posted On :2017-09-15 16:39:22.0
Leave a Comment
Hi Isha,
I see you did some typo error while typing the code on your machine. Please initialize boolean value false not False. (it should be in small case)

Thanks
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1862  20593
Posted On :2017-09-16 15:56:33.0
Leave a Comment
search.java:3:error:class search is public, should be declared in a file named search.java
public class search <
^
1 error
This is what I got sir
profile Rajavenkatesh M - anyforum.in Rajavenkatesh M
0  0  0
Posted On :2017-11-25 19:30:08.0
Leave a Comment
I did a small mistake. now I find it
it´s working
thank you
profile Rajavenkatesh M - anyforum.in Rajavenkatesh M
0  0  0
Posted On :2017-11-25 19:33:24.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in