How to break a number into separate digits in Fortran

  • #1
21
2
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))
  read*,x
  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:

Answers and Replies

  • #2
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,668
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))
  read*,x
  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:
  • #3
NascentOxygen
Staff Emeritus
Science Advisor
9,244
1,072
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
 
  • #4
DrClaude
Mentor
7,538
3,860
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.
 
  • #5
21
2
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'


  read(*,*,iostat=st)x
  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
 
  • #6
21
2
Here is line 34
harmonicmean=(5)/((1/div11)+(1/div22)+(1/div33)+(1/div44)+(1/div55))
 
  • #7
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,668
Here is line 34
harmonicmean=(5)/((1/div11)+(1/div22)+(1/div33)+(1/div44)+(1/div55))
In Fortran, when doing INTEGER division, if INTA / INTB < 0, then 0 is returned as the quotient. In your calculation of the harmonic mean, if div11 ... div55 are all > 1, then the sum in the denominator will equal 0, and you'll get an error.
 

Related Threads on How to break a number into separate digits in Fortran

Replies
2
Views
3K
Replies
1
Views
2K
Replies
7
Views
3K
  • Last Post
Replies
5
Views
4K
Replies
12
Views
2K
Replies
0
Views
1K
  • Last Post
Replies
7
Views
858
Replies
5
Views
1K
Replies
7
Views
2K
Top