
Sam W. answered 02/02/24
Master's in Nuclear Astrophysics: all 4 years were done in Fortran
This is a nice, simple program on Fortran.
You can use DO LOOPS - these functions loop around a specified amount of times.
I am not sure how advanced you are in Fortran, but the understanding is the most important part.
Then you can use the syntax, which is the easy part to learn.
Here is what you want your loop to do:
- Create a variable that is the one that will hold the sum.
- Loop 100 times.
- Each loop, add the current number onto the sum of the number before.
- Print out the final answer.
Very quickly, you will need to set it up like this:
PROGRAM sumto100
IMPLICIT none
INTEGER :: n, sum
!set your variables
sum = 0 !used to find the sum
!set up your do loop to loop over every number from 1-100
do n=1, 100
sum = sum + n
end do
WRITE(6,*) 'The sum of all the numbers from 1 - 100 is:', sum
END PROGRAM