techhipbettruvabetnorabahisbahis forumutaraftarium24edueduedueduseduseduedusedusedusedus Java : Check if an Array Contains a Given Number | Java Programs
Latest :

Java : Check if an Array Contains a Given Number | Java Programs

Here is we have to check if an array contains a value Java Program. Along with that, we will provide you with suitable examples and sample output.

This code is for checking whether a user-determined element is present in an array using Java language.

  • The problem here is to check whether a given element is present in an array.
  • Our constraint here is the ability to only check for the presence of int values.
  • The input here is a int value taken from the user.
  • The output is a boolean value of true or false representing whether the input int value is present in the array.

Output – 1:

  1. The main method begins by declaring two int variables and c. 
  2. A new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java.
  3. Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class.
  4. The next line prints a statement to the console instructing the user to enter the number of elements (i.e. the size of the array to be defined).
  5. The .nextInt() method is called by the Scanner object sc. This method reads an int value input by a user and stores it to the int variable n.
  6. Array is initialized having size and the user is asked to enter number of elements and as the loop runs, each input int value is stored in position of that iteration in the array a contains given number.
  7. The next line prints a statement to the console instructing the user to enter a number to check whether it is present in the array.
  8. The .nextInt() method is called by the Scanner object sc. This method reads an int value input by a user and stores it to the int variable c.
  9. In the next few lines, each element in the array i.e. all the user-defined elements is printed out to the console and within the print statement in the final line, the method check is called with array a and intpassed in as arguments.
  10. In this method, a for loop iterates over each element of the array unless the element being checked is found in the array in which case, the implementation of the method terminates.
  11. Within each iteration of the loop, the conditional statement checks whether the int variable check is equal in value to the element present at position in the array.
  12. If this condition is satisfied for any iteration of the loop, the method returns a boolean value of true. In the case that the conditional statement is not satisfied for any of the iteration, a boolean value of false is returned.
  13. This returned boolean value is printed out by the print statement in the last line of the main method to see if the array contains a value Java.
  • Here, the element to check is the number 3. The number 3 is present at index position 2 in the array.
  • The conditional statement in the method check gets satisfied in the third iteration of the loop and hence a boolean value of true is printed out.

Output – 2:

  • Here, the element to check is the number 3. The number 3 is not present at any index position in the array contains a given number.
  • The conditional statement in the method check does not get satisfied in any iteration of the loop and hence a boolean value of false is printed out.

techhipbettruvabetnorabahisbahis forumutaraftarium24edusedueduedueduedusedueduedusedus
x

Check Also

Reverse A Number In Java – 4 Simple Ways | Programs

Reverse A Number In Java – We have discussed the various methods to reverse a ...

betwoonhacklink satın albets10 girişakcebet uyeliksupertotobet1xbet girişmelbet girişbetwinnerpinup uyelikbtcbahis girişmostbet girişbetwinner girişxslotskyloft uyelikmegapari uyeliktipobetgrandpashabetradissonbetmariobett.mebetandreasenbetcasivalmisbahismyhitbetonbahistipobetbahsinevavepasgolgrandpashabetmarsbahispusulabetcasibomcasibom girişcasibom güncel girişcasibomcasibom girişdeneme bonusu veren sitelerhttps://mobilebarhawaii.com/ballettea tropazmegabahis güncel girişmatadorbetbets10betturkeycasibomkingroyalbets10 girişbets10bets10maldives casinohacklinkgrandpashabetbetmatik girişmelbetmostbetmegapari üyelikistanbul escortmatadorbetjojobetJojobetjojobetpinco girisonwinonwin girişcasibom güncel girişselcuksportshdcasibomcasibomatakoy escortatakoy escortsisli escortgalatabahisliste paris sportifjeux casino gratuit avec bonusSimsinos Casinojojobet girişCashimashi AvisWunderWinsparier sur les cornersGamdomCasibommadridbet girişatlasbetpinbahisemiratesbettrbetcasibomcratosroyalbetpalacebetcasinokaliprensbetmostbet guncel girisbetandyou guncel girisromabet guncel giris7slots guncel girisbizbet guncel girisrocksroyalbetkazandradeneme bonusu veren sitelergrandpashabetonwinprincessbethttp://nakanakadining.com/betkanyonpinup yeni girismatadorbet yeni girisroketbet yeni girisskyloft yeni girismegapari yeni girissavoybettingcasinomhubligobetmavibetenobahisJojobetpusulabetjetbetonwin girişkingroyal girişarnavutköy escortkingroyalmaltcasinoholiganbet girişsahabetjojobethttps://www.escortbayanlariz.net/beylikdüzü escortfikstürbetmisliwinmarsbahisbets10meritkingsekabet girişvbetsahabet girişholiganbetmeritkingholiganbet girişholiganbet girişjojobetjojobetbets10Jojobet Girişmariobetodeonbetodeonbetbetwoonjojobetsahabetkingroyalbets10 girişjojobetmeritking girişsekabetmeritbetcasibomcasibom girişpinbahiszbahiscasinometropolcasinomaxiarnavutköy escortGebze escort matbetzbahiscasinomaximarsbahismarsbahisbetebetcasibomperabetbakırköy escortmatbet girişbelugabahissüperbetin güncel girişjojobetpusulabetMavibetOnwinbets10 girişcasibomextrabetcasibomdeneme bonusu veren sitelerholiganbet girişbetturkey girişbetturkeyhalkali escortmobilbahisasyabahiscasibom güncel girişsahabetjojobetjojobetdumanbetmadridbetnakitbahisultrabetultrabetotobetkulisbetfixbetcratosslotbetturkeykralbetholiganbetmatbetcasibom girişjojobet girişpadişahbetbets10 girişbets10onwinselçuksportsSekabetholiganbethttps://www.gvscolombia.com/güvenilir casino siteleri antalya escortbetkanyonjojobetdumanbetganobetganobet girişiptviptvcashbackiptvankara escort bayaninterbahiscasibomgrandpashabetcasival girişmarsbahismatadorbetbetofficeJojobetmaldives betcasibom girişmeritkingbetciojojobet girişgrandpashabet girişbahiscasino girişmegabahis güncel girişbetorderBankobetBetordercasibom 811 giriştaraftarium24holiganbetholiganbetholiganbetjojobetzbahisGrandpashabet güncel girişcasibomjojobetonwinextrabetmatbetextrabet girişmatbet girişbetwoonmarsbahisbets10TipobetpusulabetSekabetcasibomesenyurt anal escortrus escortholiganbet girişGrandpashabetholiganbetcasinomaxicasibomgüvenilir casino siteleri grandpashabetİstanbul Escortbetciohttps://www.gvscolombia.com/marsbahisbetciodeneme bonusu veren sitelerotobetcasinomaximarsbahis girişstarzbetjojobetjojobet girişmrbahismrbahisJojobetsahabetjojobetjojobet girişgooglecasibomkucukcekmece escortsekabetsahabetrestbetpinbahisonwinmeritbetmavibetmatbetmatadorbetmaltcasinokingroyalimajbetholiganbetgrandpashabetizmir escorttexas sweepstakesflorida sweepstakessweepstakes sportsbooksweepslotsbetgaranti girişbetgarantibankobet girişPusulabet güncel girişmobilbahisbetciorestbetcasibomgrandpashabetmatadorbetmaltcasinoholiganbetmarsbahisonwinsahabetsekabetmatbetimajbetsekabet girişvaycasinotuzla escort bayancanlı bahiscasibomimajbetstarzbetmobil ödeme bozdurmajojobet