FORTRAN ROGRAM THAT CAN ACCEPT TWO INTEGER NUMBERS AND COMPUTE AND DISPLAY ALL THEIR COMMON PRIME FACTORS

Surprised some school actually programmes in Fortran. NASA still does though. It's been a while since I last touched the language. If you have written the programme and run into difficulties, I can help you debug, syntax or logic.

In fortran,
read(5,*)I,J
will read in two integers in free format.
write(6,'2I5')I,J
will write I and J up to 5 characters each.

You can use Euclid's algorithm to find the highest common factor, namely:
1. find the difference between I and J, call it K.
2. Divide the smaller of I or J by K and call the remainder R.
3. Replace the bigger of I or J by R and repeat 1 & 2 until R=0 or 1.
If R=0, K is the GCF.
If R=1, the GCF is 1 (i.e. no common factors).

Example:
I=80
J=48
K=I-J=32
R=remainder of 48/32=1 R16

I=16
J=48
K=48-16=32
R=remainder of 16/32=0 R16
I=16
J=16
GCF = 16 = 2^4

thanks mathmate for your response.

we were asked to use WATFOR77 compiler.i'm a newbee as far as programming is concern.

can you pls. write the program the way i can run it in WATFOR77?

thanks

edwin

To write a Fortran program that accepts two integer numbers and computes and displays their common prime factors, you can follow these steps:

1. Declare the necessary variables. You'll need two variables to store the input numbers and an array to store the common prime factors.

```fortran
INTEGER :: num1, num2
INTEGER, DIMENSION(:), ALLOCATABLE :: common_factors
```

2. Prompt the user to enter the two numbers and store them in the variables `num1` and `num2`.

```fortran
WRITE(*,*) 'Enter the first number:'
READ(*,*) num1

WRITE(*,*) 'Enter the second number:'
READ(*,*) num2
```

3. Create a subroutine `is_prime` to check if a given number is prime.

```fortran
SUBROUTINE is_prime(n, isPrime)
INTEGER, INTENT(IN) :: n
LOGICAL, INTENT(OUT) :: isPrime
INTEGER :: i
isPrime = .TRUE.
DO i = 2, n/2
IF (MOD(n, i) == 0) THEN
isPrime = .FALSE.
EXIT
END IF
END DO
END SUBROUTINE is_prime
```

4. Create a subroutine `get_common_prime_factors` to find the common prime factors between two numbers.

```fortran
SUBROUTINE get_common_prime_factors(num1, num2, common_factors)
INTEGER, INTENT(IN) :: num1, num2
INTEGER, DIMENSION(:), ALLOCATABLE, INTENT(OUT) :: common_factors
INTEGER :: i, k
ALLOCATE(common_factors(MAX(num1,num2)))
k = 1

DO i = 2, MIN(num1, num2)
IF (MOD(num1, i) == 0 .AND. MOD(num2, i) == 0) THEN
LOGICAL :: isPrime
CALL is_prime(i, isPrime)
IF (isPrime) THEN
common_factors(k) = i
k = k + 1
END IF
END IF
END DO

common_factors = common_factors(1:k-1)
END SUBROUTINE get_common_prime_factors
```

5. Call the `get_common_prime_factors` subroutine with the input numbers and display the common prime factors.

```fortran
CALL get_common_prime_factors(num1, num2, common_factors)
WRITE(*,*) 'Common prime factors:'
DO i = 1, SIZE(common_factors)
WRITE(*,*) common_factors(i)
END DO
```

The complete Fortran program would look like this:

```fortran
PROGRAM CommonPrimeFactors
INTEGER :: num1, num2
INTEGER, DIMENSION(:), ALLOCATABLE :: common_factors

! Prompt the user to enter the numbers
WRITE(*,*) 'Enter the first number:'
READ(*,*) num1

WRITE(*,*) 'Enter the second number:'
READ(*,*) num2

! Check for common prime factors
CALL get_common_prime_factors(num1, num2, common_factors)

! Display the common prime factors
WRITE(*,*) 'Common prime factors:'
DO i = 1, SIZE(common_factors)
WRITE(*,*) common_factors(i)
END DO

CONTAINS

SUBROUTINE is_prime(n, isPrime)
INTEGER, INTENT(IN) :: n
LOGICAL, INTENT(OUT) :: isPrime
INTEGER :: i

isPrime = .TRUE.
DO i = 2, n/2
IF (MOD(n, i) == 0) THEN
isPrime = .FALSE.
EXIT
END IF
END DO
END SUBROUTINE is_prime

SUBROUTINE get_common_prime_factors(num1, num2, common_factors)
INTEGER, INTENT(IN) :: num1, num2
INTEGER, DIMENSION(:), ALLOCATABLE, INTENT(OUT) :: common_factors
INTEGER :: i, k

ALLOCATE(common_factors(MAX(num1,num2)))
k = 1

DO i = 2, MIN(num1, num2)
IF (MOD(num1, i) == 0 .AND. MOD(num2, i) == 0) THEN
LOGICAL :: isPrime
CALL is_prime(i, isPrime)
IF (isPrime) THEN
common_factors(k) = i
k = k + 1
END IF
END IF
END DO

common_factors = common_factors(1:k-1)
END SUBROUTINE get_common_prime_factors

END PROGRAM CommonPrimeFactors
```

This program will accept two integer numbers from the user and display their common prime factors.