# How to break a number into separate digits in Fortran

• Comp Sci
Mod note: Fixed indents in code below
Fortran:
!this program accepts a 5 digit integer
!and calculates the harmonic and geometric means of it's digits
!the program checks and denies numbers with lenght diffrent than 5 digits
!The harmonic mean should be calculated only when
!all of the number's digits are non zero
program ex_3
implicit none
!Variables
integer::harmonicmean,geometricmean,div,div1,div2,div3,div4,div5,x
print*,'Welcome to my program,please input a 5 digit number'
geometricmean=(div1*div2*div3*div4*div5)**(1/5)
harmonicmean=(5)/((1/div1)+(1/div2)+(1/div3)+(1/div4)+(1/div5))
if  (x>=11111) then
if  (x<=99999) then
print*,'to apotelesma twn armonikwn kai geometrikwn meswn einai:',geometricmean,harmonicmean
end if
else
if(x>=10000) then
print*,'Impossible to calculate harmonic mean'
print*,'Geometricmean=',geometricmean
else
print*,'Error'
end if
end if
end program
1. Hello everyone! I've been trying two days to find a solution to my problem though,unfortunately I'm stuck.The problem demands the creation of a program that is able to calculate the geeometric and the harmonic mean of a 5-digit integer.In addition the program checks and denies with approriate message numbers other than 5 digits in lenght.Ultimately the harmonic mean should be calculated only when all of the 5 digits are diffrent than 0.

So what is really baffling me is how to break apart a 5 digit number and use it's digits for my calculations.I know that the code is not right though I assume it will much easier if a solve this question.I've written a few codes though none of them seems right to me.I would really appreciate your help,thanks in advance

## The Attempt at a Solution

Last edited by a moderator:

Related Engineering and Comp Sci Homework Help News on Phys.org
SteamKing
Staff Emeritus
Homework Helper
Mod note: Fixed indents in code below
Fortran:
!this program accepts a 5 digit integer
!and calculates the harmonic and geometric means of it's digits
!the program checks and denies numbers with lenght diffrent than 5 digits
!The harmonic mean should be calculated only when
!all of the number's digits are non zero
program ex_3
implicit none
!Variables
integer::harmonicmean,geometricmean,div,div1,div2,div3,div4,div5,x
print*,'Welcome to my program,please input a 5 digit number'
geometricmean=(div1*div2*div3*div4*div5)**(1/5)
harmonicmean=(5)/((1/div1)+(1/div2)+(1/div3)+(1/div4)+(1/div5))
if  (x>=11111) then
if  (x<=99999) then
print*,'to apotelesma twn armonikwn kai geometrikwn meswn einai:',geometricmean,harmonicmean
end if
else
if(x>=10000) then
print*,'Impossible to calculate harmonic mean'
print*,'Geometricmean=',geometricmean
else
print*,'Error'
end if
end if
end program
1. Hello everyone! I've been trying two days to find a solution to my problem though,unfortunately I'm stuck.The problem demands the creation of a program that is able to calculate the geeometric and the harmonic mean of a 5-digit integer.In addition the program checks and denies with approriate message numbers other than 5 digits in lenght.Ultimately the harmonic mean should be calculated only when all of the 5 digits are diffrent than 0.

So what is really baffling me is how to break apart a 5 digit number and use it's digits for my calculations.I know that the code is not right though I assume it will much easier if a solve this question.I've written a few codes though none of them seems right to me.I would really appreciate your help,thanks in advance

## The Attempt at a Solution

Remember, decimal numbers are representations of powers of ten placed in position such that the most significant digits are to the left.

For example, the number 54627 = 5×104 + 4×103 + 6x102 + 2×101 + 7×100

One method:

If you divide 54627 by 104, you're going to get 5.4627, and you should be able to find a Fortran intrinsic function which can give you the integer part of this result.
Once you determine the left-most digit, you should be able to calculate the residual number, 4627, and repeat the process until all of the digits in the original number have been determined.

There are other methods which may be employed, such as reading the input number as a character string and identifying each digit from the string.

Last edited by a moderator:
NascentOxygen
Staff Emeritus
You could convert your integer to digits by using a method based around integer division, e.g., if you divide 34567 by 10000 you get 3, and if you divide 34567 by 1000 you get 34, etc.

An alternative involves manipulating the data as character strings. Functions you'll need can be found by google searches. Look for converting integers to strings in FORTRAN, then you'll need string operations in FORTRAN.

http://www.oc.nps.edu/~bird/oc3030_online/fortran/basics/basics.html

DrClaude
Mentor
Fortran:
geometricmean=(div1*div2*div3*div4*div5)**(1/5)
harmonicmean=(5)/((1/div1)+(1/div2)+(1/div3)+(1/div4)+(1/div5))
Note that you are performing these calculations before div1...div5 have been assigned a value. Also, since these are all variables are of thpe integer, you will get probably get back only zeros.

Thank you all for your answers I really appreciate that.I have updated the code though the only error it seems have is that in line 34 it occurs a problem that says integer diveded by zero,although I contemplate there is no such division.Is anyone able to spot what's wrong?
Fortran:
!this program accepts a 5 digit integer
!and calculates the harmonic and geometric means of it's digits
!the program checks and denies numbers with lenght diffrent than 5 digits
!The harmonic mean should be calculated only when
!all of the number's digits are non zero
program ex_3
implicit none
!Variables
integer::st=0
integer::harmonicmean,geometricmean,div1,div2,div3,div4,div5,x
integer::div11,div22,div33,div44,div55
print*,'Welcome to my program,please input a 5 digit number'

if (st/=0) then
print*,'An Error has occured'
stop
else
if  (x>=11111) then
if  (x<=99999) then

div1=((x/10000))
div11=(mod(div1,10000))
div2=(x/1000)
div22=(mod(div2,1000))
div3=((x/100))
div33=((mod(div3,100)))
div4=((x/10))
div44=((mod(div4,10)))
div5=((x))
div55=(mod(div5,10))
geometricmean=(div11*div22*div33*div44*div55)**(1/5)
harmonicmean=(5)/((1/div11)+(1/div22)+(1/div33)+(1/div44)+(1/div55))
print*,'to apotelesma twn armonikwn kai geometrikwn meswn einai:',real(geometricmean),real(harmonicmean)
end if

else
if(x>=10000) then
print*,'Impossible to calculate harmonic mean'
print*,'Geometricmean=',geometricmean
else
print*,'Error'
end if
end if
end if
end program

Here is line 34
harmonicmean=(5)/((1/div11)+(1/div22)+(1/div33)+(1/div44)+(1/div55))

SteamKing
Staff Emeritus