fortran 输入年份判断是否为闰年
时间: 2024-06-15 11:05:02 浏览: 17
Fortran是一种编程语言,可以用来编写科学计算和数值分析的程序。下面是一个Fortran程序示例,用于判断输入的年份是否为闰年:
```fortran
program leap_year
implicit none
integer :: year
! 从用户输入获取年份
write(*,*) "请输入一个年份:"
read(*,*) year
! 判断是否为闰年
if (mod(year, 4) == 0) then
if (mod(year, 100) /= 0 .or. mod(year, 400) == 0) then
write(*,*) year, "是闰年"
else
write(*,*) year, "不是闰年"
end if
else
write(*,*) year, "不是闰年"
end if
end program leap_year
```
上述程序首先会要求用户输入一个年份,然后通过使用`mod`函数来判断该年份是否为闰年。如果能被4整除但不能被100整除,或者能被400整除,则判断为闰年。
相关问题
怎样用fortran判断一年是否为闰年
可以使用以下代码来判断一年是否为闰年:
```fortran
function is_leap_year(year)
logical :: is_leap_year
integer, intent(in) :: year
is_leap_year = (mod(year, 4) == 0) .and. ((mod(year, 100) /= 0) .or. (mod(year, 400) == 0))
end function is_leap_year
```
其中,`year` 为需要判断的年份,函数返回值为逻辑值,表示该年是否为闰年。
fortran语言判断字符串是否为回文
下面是一个简单的Fortran程序,用于判断一个字符串是否是回文:
```
program palindrome
implicit none
character(len=100) :: str
integer :: i, len
logical :: is_palindrome
write(*,*) "Enter a string: "
read(*,'(A)') str
len = len_trim(str)
is_palindrome = .true.
do i=1, len/2
if(str(i:i) /= str(len-i+1:len-i+1)) then
is_palindrome = .false.
exit
endif
enddo
if(is_palindrome) then
write(*,*) "The string is a palindrome."
else
write(*,*) "The string is not a palindrome."
endif
end program palindrome
```
在这个程序中,我们首先读入一个字符串,然后通过循环比较字符串的第一个字符和最后一个字符、第二个字符和倒数第二个字符,以此类推。如果所有字符都匹配,则字符串为回文。如果有任何一对字符不匹配,则字符串不是回文。