Fortran - Arithmetic Operators


Advertisements

Following table shows all the arithmetic operators supported by Fortran. Assume variable A holds 5 and variable B holds 3 then −

Operator Description Example
+ Addition Operator, adds two operands. A + B will give 8
- Subtraction Operator, subtracts second operand from the first. A - B will give 2
* Multiplication Operator, multiplies both operands. A * B will give 15
/ Division Operator, divides numerator by de-numerator. A / B will give 1
** Exponentiation Operator, raises one operand to the power of the other. A ** B will give 125

Example

Try the following example to understand all the arithmetic operators available in Fortran −

program arithmeticOp

! this program performs arithmetic calculation
implicit none  

   ! variable declaration
   integer :: a, b, c
   
   ! assigning values 
   a = 5   
   b = 3  
   
   ! Exponentiation 
   c = a ** b 
   
   ! output 
   print *, "c = ", c
   
   ! Multiplication  
   c = a * b 
   
   ! output 
   print *, "c = ", c
   
   ! Division  
   c = a / b 
   
   ! output 
   print *, "c = ", c
   
   ! Addition
   c = a + b 
   
   ! output 
   print *, "c = ", c
   
   ! Subtraction 
   c = a - b 
   
   ! output 
   print *, "c = ", c
   
end program arithmeticOp

When you compile and execute the above program, it produces the following result −

c = 125
c = 15
c = 1
c = 8
c = 2
fortran_operators.htm
Advertisements