Download CMPS 401

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

C Sharp (programming language) wikipedia , lookup

Sieve of Eratosthenes wikipedia , lookup

Transcript
CMPS 401
Programming Assignment IV
C# Assignment
Fall 2004
DUE DATE (TUESDAY NOVEMBER 4, 2004)
Assignment (20 pts)

You are to convert the following Java program into a C# one.
// Print all prime numbers between 2 and 10000.
// Display 8 prime numbers per line.
public class Exercise3_9 {
// Main method
public static void main(String[] args) {
int count = 1; // Count the number of prime numbers
int number = 2; // A number to be tested for primeness
boolean isPrime = true; // If the current number is prime?
System.out.println("The first 50 prime numbers are \n");
// Repeatedly test if a new number is prime
while (number <= 10000) {
// Assume the number is prime
isPrime = true;
// Set isPrime to false, if the number is prime
for (int divisor = 2; divisor <= number / 2; divisor++) {
if (number % divisor == 0) { // If true, the number is prime
isPrime = false;
break; // Exit the for loop
}
}
// Print the prime number and increase the count
if (isPrime) {
if (count%8 == 0) {
// Print the number and advance to the new line
System.out.println(number);
}
else
System.out.print(number + " ");
count++;
// Increase the count
}
// Check if the next number is prime
number++;
}
}
}

The Java program above compiles and runs perfectly using jGRASP.