以下是使用 Fortran 编写的程序,用于打印满足条件的素数:

program prime_numbers
    implicit none
    integer :: num, i, j, sum, remainder
    logical :: is_prime

    print*, 'Prime numbers between 100 and 999 satisfying the given condition:'
    
do num = 100, 999
        sum = (num / 10) % 10 + num % 10
        remainder = num / 100

        ! Check if the number is prime
        is_prime = .true.
        do i = 2, num-1
            if (mod(num, i) == 0) then
                is_prime = .false.
                exit
            end if
        end do

        ! Print the number if it satisfies the condition and is prime
        if (sum % 10 == remainder .and. is_prime) then
            print*, num
        end if
    end do

end program prime_numbers

该程序通过循环遍历 100 到 999 之间的数,对每个数进行如下操作:

  1. 计算该数的个位数字与十位数字之和除以 10 所得的余数。
  2. 判断该数是否为素数,即除了 1 和自身外没有其他因子。
  3. 如果该数满足条件,并且是素数,则将其打印输出。

你可以运行这个程序,并检查输出结果是否满足条件。

Fortran 程序:打印满足条件的素数 (100-999)

原文地址: https://www.cveoy.top/t/topic/vUb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录