0% found this document useful (0 votes)
63 views3 pages

Tugas Algoritma Dan Pemrogaman: A. Saverina A. 076

The document discusses three Java program listings that demonstrate different algorithms: 1. A program that demonstrates pass-by-value by attempting to swap two integer variables using a method. The variables are not actually swapped. 2. A program that calculates the greatest common divisor (GCD) of two integers input by the user using a recursive GCD method. 3. A program that prints the first 50 prime numbers by using a method to check if a number is prime and repeatedly testing numbers starting from 2.

Uploaded by

saverina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views3 pages

Tugas Algoritma Dan Pemrogaman: A. Saverina A. 076

The document discusses three Java program listings that demonstrate different algorithms: 1. A program that demonstrates pass-by-value by attempting to swap two integer variables using a method. The variables are not actually swapped. 2. A program that calculates the greatest common divisor (GCD) of two integers input by the user using a recursive GCD method. 3. A program that prints the first 50 prime numbers by using a method to check if a number is prime and repeatedly testing numbers starting from 2.

Uploaded by

saverina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

TUGAS ALGORITMA DAN PEMROGAMAN

A. Saverina A.
076

Membuat program untuk :


1. Listing 5.5

package test.pass.by.value;
public class TestPassByValue {
/*main method*/
public static void main(String[] args) {
// Declare and initialize variables
int num1 = 1;
int num2 = 2;

System.out.println("Before invoking the swap method, num1 is " + num1 + " and
num2 is " + num2);
// Invoke the swap method to attempt to swap two variables
swap(num1, num2);
System.out.println("After invoking the swap method, num1 is " + num1 + " and
num2 is " + num2);
}

/** Swap two variables */


public static void swap(int n1, int n2){
System.out.println("\tInside the swap method");
System.out.println("\t\tBefore swapping, n1 is " + n1 + " and n2 is " + n2);

// Swap n1 with n2
int temp = n1;
n1 = n2;
n2 = temp;
System.out.println("\t\tAfter swapping, n1 is " + n1 + " and n2 is " + n2);

2. Listing 5.6
package greatest.common.divisor.method;
import java.util.Scanner;
public class GreatestCommonDivisorMethod {

public static void main(String[] args) {


// Create a Scanner
Scanner input = new Scanner(System.in);

// Prompt the user to enter two integers


System.out.print("Enter first integer: ");
int n1 = input.nextInt();
System.out.print("Enter second integer: ");
int n2 = input.nextInt();

System.out.println("The greatest common divisor for " + n1 + " and " + n2 + " is "+
gcd(n1, n2));
}

/** Return the gcd of two integers */


public static int gcd(int n1, int n2) {
int gcd = 1; // Initial gcd is 1
int k = 2; // Possible gcd

while (k <= n1 && k <= n2) {


if (n1 % k == 0 && n2 % k == 0)
gcd = k; // Update gcd
k++;
}
return gcd; // Return gcd
}
}

3. Listing 5.7

package prime.number.method;
public class PrimeNumberMethod {

public static void main(String[] args) {


System.out.println("The first 50 prime numbers are \n");
printPrimeNumbers(50);
}

public static void printPrimeNumbers(int numberOfPrimes) {


final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line
int count = 0; // Count the number of prime numbers
int number = 2; // A number to be tested for primeness

// Repeatedly find prime numbers


while (count < numberOfPrimes) {
// Print the prime number and increase the count
if(isPrime(number)) {
count++; // Increase the count
//}
if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
// Print the number and advance to the new line
System.out.printf("%-5s\n", number);
}
else
System.out.printf("%-5s", number);
}
// Check whether the next number is prime
number++;
}
}
/** Check whether number is prime */
public static boolean isPrime(int number){
for (int divisor = 2; divisor <= number / 2; divisor++) {
if (number % divisor == 0) { // If true, number is not prime
return false; // Number is not a prime
}
}
return true; // Number is prime
}
}

You might also like