{"id":1317,"date":"2016-07-27T07:56:01","date_gmt":"2016-07-27T07:56:01","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1317"},"modified":"2024-12-13T15:40:00","modified_gmt":"2024-12-13T15:40:00","slug":"write-a-java-program-to-find-if-a-number-is-even-or-odd","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-to-find-if-a-number-is-even-or-odd\/","title":{"rendered":"Java program to find if a number is even or odd?"},"content":{"rendered":"<h2>Java program to find if a number is even or odd?<\/h2>\n<p>Even or Odd: If the number is divisible by 2 then it is even, otherwise it is an odd number. On this page, we will write a Java program that checks if a number is odd or even.<\/p>\n<h2><strong>Mathematical Definition<\/strong><\/h2>\n<p>The formal mathematical definition of an even number is that it is an integer of the form n = 2i, where i is an integer. An odd number is an integer of the form n = 2i + 1.<\/p>\n<p>even\u00a0 =\u00a0\u00a0 { 2i\u00a0 such that where i belongs to Z<br \/>\nodd\u00a0\u00a0\u00a0 =\u00a0\u00a0 { 2i + 1 such that where i belongs to Z<\/p>\n<p>&nbsp;<\/p>\n<h2>Raptor Flowchart<\/h2>\n<p><strong><a href=\"https:\/\/www.testingdocs.com\/questions\/find-a-number-is-even-or-odd-raptor-flowchart\/\">https:\/\/www.testingdocs.com\/questions\/find-a-number-is-even-or-odd-raptor-flowchart\/<\/a><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-713\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Even_Odd_Number.jpeg\" alt=\"Even Odd\" width=\"1159\" height=\"622\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Even or Odd Java program<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.Scanner;\r\n\r\npublic class EvenOddExample {\r\n public static void main(String args[])\r\n {\r\n System.out.println(\"Enter number to check if its odd or even\");\r\n Scanner in = new Scanner(System.in);\r\n int x = in.nextInt();\r\n if(x % 2 == 0)\r\n System.out.println(\"The number is an even number\");\r\n else\r\n System.out.println(\"The number is an odd number\");\r\n\r\nin.close();\r\n }\r\n\r\n}<\/pre>\n<h3><strong>Output of the program:<\/strong><\/h3>\n<p><strong>Testcase 1<\/strong><\/p>\n<p>Enter number to check if its odd or even<br \/>\n20<br \/>\nThe number is an even number<\/p>\n<p><strong>TestCase2<\/strong><\/p>\n<p>Enter number to check if its odd or even<br \/>\n77<br \/>\nThe number is an odd number<\/p>\n<p>&nbsp;<\/p>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/EvenOdd-Run-Output.jpeg\" alt=\"EvenOdd Run Output\" width=\"849\" height=\"717\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>We have used the modulus operator to find the remainder in the java program. It is an important arithmetical operator also known as a remainder operator. It returns the remainder of the two numbers. For instance 5 % 2 is 1 because 5 divided by 2 leaves a remainder of 1. In this program, we check for n % 2\u00a0 is 0 i.e nothing left after dividing the number by 2. So the number is an even number.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-769\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Modulus-operator-in-Java.jpeg\" alt=\"Modulus operator in Java\" width=\"1164\" height=\"626\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Java Tutorial on this website:<\/p>\n<ul>\n<li><strong><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/strong><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java program to find if a number is even or odd? Even or Odd: If the number is divisible by 2 then it is even, otherwise it is an odd number. On this page, we will write a Java program that checks if a number is odd or even. Mathematical Definition The formal mathematical definition [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-1317","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1317"}],"version-history":[{"count":8,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1317\/revisions"}],"predecessor-version":[{"id":26312,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1317\/revisions\/26312"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}