-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFirst.java
More file actions
44 lines (37 loc) · 914 Bytes
/
Copy pathFirst.java
File metadata and controls
44 lines (37 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Every program must be in a class
// the class name must be == to file name
// By convention, classes are initial-caps
class First
{
int upperLimit;
// The entry point (first function called) for all programs is main
/*public static void main(String[] args)
{
System.out.println("Welcome to the class!");
upperLimit = 200;
printPrimesUpTo();
}*/
// void return type means we operate solely by side-effects
// No return value.
void printPrimesUpTo()
{
System.out.println("Here are the primes up to ".toUpperCase() + upperLimit);
for(int i = 0; i <= upperLimit; i++)
{
if(isPrime(i))
{
System.out.print(i + " ");
}
}
}
// Return true if x is prime, otherwise false
boolean isPrime(int x)
{
if(x<2) return false;
for(int d = 2; d < x; d++)
{
if(x % d == 0) return false;
}
return true;
}
}