Homework 3 Code

Homework 3 Code - file:/X|/cs101/Homework3_0.txt /* = Solve...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
file:///X|/cs101/Homework3_0.txt /* == Solve 4.20 from page 125 == Write a program that will print all the prime numbers between 2 and 100, inclusively. Display eight prime numbers per line. */ public class Homework3_0 { public static void main(String[] args) { int i, count = 0; for (i=2;i<=100;i++) { // if the number isn't prime, go to the next iteration. if (!is_prime(i)) continue; // if we've already printed 7 numbers on this line, add a // \n and reset the count. if (count == 7) { System.out.println(i); count = 0; continue; } // otherwise, print the number with a trailing tab. System.out.print(i+"\t"); // increment the count. count++; } // no errors. System.exit(0); } private static boolean is_prime(int i) { // 2 is prime. if (i == 2) return true; // 1 isn't prime nor is anything less than it. // any even number > 2 isn't prime. if (i <= 1 || i % 2 == 0) return false; // only go up to the sqrt of i, anything mre is redundant. int n;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Homework 3 Code - file:/X|/cs101/Homework3_0.txt /* = Solve...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online