Fortran 90 Program Factorial: Fixing Incorrect Output

In summary: But you must use a modern Fortran compiler.In summary, the conversation discusses a program for calculating factorials in Fortran. The code uses implicit none and a do loop to calculate the factorial of a given integer. It also mentions the use of Fortran90 intrinsic functions and recursive attributes for computing larger factorials. The conversation notes that for very large numbers, the result may not be accurate and suggests using a modern compiler and using a result of double or extended real type.
  • #1
Md. Abde Mannaf
20
1
Code:
program factorial
  implicit none
  integer::fact,i,n
  print*,'enter the value of n'
  read*,n
  fact=1
  do i=1,n
  fact=fact*i
  end do

  print*,'factorial is ',fact
end program

when input n largest number then answer is incorrect. how to solve
 
Technology news on Phys.org
  • #2
Md. Abde Mannaf said:
Code:
program factorial
  implicit none
  integer::fact,i,n
  print*,'enter the value of n'
  read*,n
  fact=1
  do i=1,n
  fact=fact*i
  end do

  print*,'factorial is ',fact
end program

when input n largest number then answer is incorrect. how to solve

Show us what you get for the first few possibilities for n:

1
2
3
4
5
6

etc.
 
  • #3
Your code looks good to me. When you say the answer is incorrect for "the largest number", don't forget that fact will be larger than the largest allowable integer in that case. The program will either abort or give an incorrect answer. You can only input values of n that will not make n! too large for the integer fact.
 
  • Like
Likes berkeman
  • #4
There are implementations of the factorial function that allow the computation of factorials beyond 20!.

The implementations are much more difficult to write, but if you want to get around the storage limits, you have to be a bit more clever.
 
  • Like
Likes Md. Abde Mannaf
  • #5
You need to start using Fortran90 intrinsic functions that allow for requesting more storage in a platform compatible manner, I am talking about functions like "selected_int_kind"; in my laptop, I was able to go as high as 18. Needless to say, the factorial function is a nice textbook exercise to show another Fortran90 feature: recursive attribute.

Code:
program torial
    implicit none
   
    integer, parameter :: IK = selected_int_kind(18)
    integer(IK) :: i
     
    write(*,'(A,$)') 'Number? : '
    read(*,*) i
    write(*,*) 'Factorial is ',factorial(i)

contains

    recursive function factorial( n ) result (f)
        integer(IK) :: f, n
       
        if( n == 1 ) then
          f = 1
        else
          f = n * factorial( n - 1_IK )
        end if
       
    end function factorial
   
end program torial

> a.exe
Number? : 20
Factorial is 2432902008176640000
 
  • Like
Likes Md. Abde Mannaf
  • #6
For large numbers may you don't need exact an integer. Try the same code with result as double or extended real (16 byte float number gives answer up to 4930!).
 

What is Fortran 90 Program Factorial?

Fortran 90 Program Factorial is a computer program written in the Fortran 90 programming language that calculates the factorial of a given number. A factorial of a number is the product of all positive integers from 1 up to that number.

Why is the output of my Fortran 90 Program Factorial incorrect?

There could be several reasons for incorrect output in a Fortran 90 Program Factorial. Some common reasons include errors in the code, incorrect input, or incorrect use of variables and data types.

How can I fix the incorrect output in my Fortran 90 Program Factorial?

To fix incorrect output, you will need to carefully review your code and check for any errors. Make sure that you are using the correct syntax and data types, and that your input is accurate. You may also consider debugging your code or seeking assistance from a more experienced programmer.

What are some common mistakes to avoid when writing a Fortran 90 Program Factorial?

Some common mistakes to avoid when writing a Fortran 90 Program Factorial include using the wrong syntax, using incorrect data types, and not properly initializing variables. It is also important to carefully check your code for errors and to thoroughly test it before running it with different inputs.

Can I use Fortran 90 Program Factorial for any number or are there limitations?

Fortran 90 Program Factorial can be used for any positive integer, as long as the number is not too large to be stored in a variable. However, keep in mind that calculating the factorial of a very large number may take a long time and may require more computing power.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
Replies
11
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
621
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
Back
Top