{"id":1426,"date":"2022-03-03T19:42:43","date_gmt":"2022-03-03T19:42:43","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1426"},"modified":"2022-03-03T19:42:43","modified_gmt":"2022-03-03T19:42:43","slug":"program-for-linear-search-example","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/program-for-linear-search-example\/","title":{"rendered":"Program for linear search \u2013 Example"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Example Program:<\/h4>\n\n\n\n<p>This program uses\u00a0linear search algorithm\u00a0to find out a number among all other numbers entered by user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Program: Linear Search Example\n * Written by: Chaitanya from beginnersbook.com\n * Input: Number of elements, element's values, value to be searched\n * Output:Position of the number input by user among other numbers*\/\nimport java.util.Scanner;\nclass LinearSearchExample\n{\n   public static void main(String args&#91;])\n   {\n      int counter, num, item, array&#91;];\n      \/\/To capture user input\n      Scanner input = new Scanner(System.in);\n      System.out.println(\"Enter number of elements:\");\n      num = input.nextInt(); \n      \/\/Creating array to store the all the numbers\n      array = new int&#91;num]; \n      System.out.println(\"Enter \" + num + \" integers\");\n      \/\/Loop to store each numbers in array\n      for (counter = 0; counter &lt; num; counter++)\n        array&#91;counter] = input.nextInt();\n\n      System.out.println(\"Enter the search value:\");\n      item = input.nextInt();\n\n      for (counter = 0; counter &lt; num; counter++)\n      {\n         if (array&#91;counter] == item) \n         {\n           System.out.println(item+\" is present at location \"+(counter+1));\n           \/*Item is found so to stop the search and to come out of the \n            * loop use break statement.*\/\n           break;\n         }\n      }\n      if (counter == num)\n        System.out.println(item + \" doesn't exist in array.\");\n   }\n}<\/code><\/pre>\n\n\n\n<p>Output 1:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter number of elements:\n6\nEnter 6 integers\n22\n33\n45\n1\n3\n99\nEnter the search value:\n45\n45 is present at location 3<\/code><\/pre>\n\n\n\n<p>Output 2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter number of elements:\n4\nEnter 4 integers\n11\n22\n4\n5\nEnter the search value:\n99\n99 doesn't exist in array.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example Program: This program uses\u00a0linear search algorithm\u00a0to find out a number among all other numbers entered by user. Output 1: Output 2:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[586],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1426"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1426"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1426\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}