1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to break a number into separate digits in Fortran

  1. Oct 27, 2015 #1
    Mod note: Fixed indents in code below
    Code (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


    3. The attempt at a solution
     
    Last edited by a moderator: Oct 27, 2015
  2. jcsd
  3. Oct 27, 2015 #2

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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: Oct 27, 2015
  4. Oct 27, 2015 #3

    NascentOxygen

    User Avatar

    Staff: Mentor

    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
     
  5. Oct 27, 2015 #4

    DrClaude

    User Avatar

    Staff: Mentor

    Code (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.
     
  6. Oct 27, 2015 #5
    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?
    Code (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
     
  7. Oct 27, 2015 #6
    Here is line 34
    harmonicmean=(5)/((1/div11)+(1/div22)+(1/div33)+(1/div44)+(1/div55))
     
  8. Oct 27, 2015 #7

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: How to break a number into separate digits in Fortran
Loading...