Otherwise it is prime<\/li>\n<\/ul>\nNotice the use of Modulus operator (%) to determine if a number is divisible by another number. This operator returns the remainder of the division of two number. eg. 19 % 4 = 3 (19 \/ 4 = 4 remainder 3)<\/p>\n
\r\npackage com.learnjava.math;\r\n\r\n\/**\r\n * Write a Java program that calculates all \r\n * prime numbers less than 100.\r\n * \r\n * @author https:\/\/learn-java-by-example.com\r\n *\r\n *\/\r\n\r\npublic class PrimeNumbers {\r\n\t\r\n\tpublic static boolean isPrime(int n) {\r\n\t\tif (n==2) {\r\n\t\t\t\r\n\t\t\t\/\/ 2 is a prime\r\n\t\t\treturn true;\r\n\t\t\t\r\n\t\t} else if (n%2 == 0) {\r\n\t\t\t\r\n\t\t\t\/\/ even numbers are not prime\r\n\t\t\t\/\/ (they are divisible by 2)\r\n\t\t\t\r\n\t\t\treturn false;\r\n\t\t\r\n\t\t} else {\r\n\t\t\t\r\n\t\t\t\/\/ Now test all possible odd divisors\r\n\t\t\t\/\/ less than square root of n\r\n\t\t\t\r\n\t\t\t\/\/ Take the square root\r\n\t\t\t\r\n\t\t\tint s = (int) Math.sqrt(n);\r\n\t\t\t\r\n\t\t\t\/\/ Loop through odd divisors 3, 5, ....\r\n\t\t\t\r\n\t\t\tfor (int i=3; i<=s; i+=2) {\r\n\t\t\t\tif (n % i == 0) {\r\n\t\t\t\t\t\r\n\t\t\t\t\t\/\/ if n is divisible by i then is is not prime\r\n\t\t\t\t\t\r\n\t\t\t\t\treturn false;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/ If we get to here then n must be a prime\r\n\t\t\t\r\n\t\t\treturn true;\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tSystem.out.println("Prime numbers:");\r\n\t\t\r\n\t\tfor (int n = 2; n < 100; n ++) {\r\n\t\t\tif (isPrime(n)) {\r\n\t\t\t\t\r\n\t\t\t\t\/\/ n is prime\r\n\t\t\t\tSystem.out.println(n);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\r\n<\/pre>\nThe solution provided here is quite inefficient and only really suitable for calculating small primes. More efficient solutions will be presented in future posts.<\/p>\n","protected":false},"excerpt":{"rendered":"
The assignment here is to calculate all prime numbers less than 100. The solution provided uses the following to determine if a given number is prime. 2 is prime Any number divisible by 2 is not prime If the number is divisible by any odd number then it is not prime Otherwise it is prime…<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4],"tags":[6,32,34,35,33,31],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-2u","jetpack-related-posts":[{"id":238,"url":"https:\/\/learn-java-by-example.com\/java\/selection-sort\/","url_meta":{"origin":154,"position":0},"title":"Selection Sort","date":"June 19, 2015","format":false,"excerpt":"When you are learning Java you are very likely going to be asked to write a program to sort values. We will start with one of the simpler sort algorithms, the Selection Sort (also known as an Exchange Sort). It's not a particularly efficient algorithm and really only suitable for\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":98,"url":"https:\/\/learn-java-by-example.com\/java\/calculator-keypad\/","url_meta":{"origin":154,"position":1},"title":"Calculator Keypad","date":"August 14, 2010","format":false,"excerpt":"This problem involves understanding some of the basics of building a Swing application including how individual components are laid out inside a window, and how your application can react to the user interacting with the application. In this case we need to update a text field whenever a button is\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":133,"url":"https:\/\/learn-java-by-example.com\/java\/fibonnaci-number\/","url_meta":{"origin":154,"position":2},"title":"Fibonnaci Number","date":"December 3, 2010","format":false,"excerpt":"Generating Fibonacci numbers is an assignment often given to Java students when they are being introduced to recursion. Recursion is programming technique that involves a method calling itself to solve a problem. When implementing a recursive solution we look for two things: A base case that returns a value without\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78,"url":"https:\/\/learn-java-by-example.com\/java\/check-palindromes-using-recursion\/","url_meta":{"origin":154,"position":3},"title":"Check Palindromes using Recursion","date":"June 1, 2011","format":false,"excerpt":"A popular problem is how to determine if a String is a Palindrome or not, the following will also assume we are only dealing with Palindromes of single words ie. no spaces or punctuation. The way to determine this is to compare the characters on the left and right ends\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":43,"url":"https:\/\/learn-java-by-example.com\/java\/palindromes\/","url_meta":{"origin":154,"position":4},"title":"Palindromes","date":"April 13, 2011","format":false,"excerpt":"A popular problem is how to determine if a String is a Palindrome or not, the following will also assume we are only dealing with Palindromes of single words ie. no spaces or punctuation. The way to determine this is to compare the characters on the left and right ends\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":160,"url":"https:\/\/learn-java-by-example.com\/java\/pascals-triangle\/","url_meta":{"origin":154,"position":5},"title":"Pascals Triangle","date":"February 13, 2011","format":false,"excerpt":"Write a Java application that prints the first 10 lines of Pascals Triangle. Each row of a Pascals Triangle can be calculated from the previous row so the core of the solution is a method that calculates a row based on the previous row which is passed as input. Once\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/154"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=154"}],"version-history":[{"count":5,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/154\/revisions"}],"predecessor-version":[{"id":158,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/154\/revisions\/158"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}