How to break a number into separate digits in Fortran

In summary: The Attempt at a SolutionRemember, 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×100One 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
  • #1
NicolasPan
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 length 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:
Physics news on Phys.org
  • #2
NicolasPan said:
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 length 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
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
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
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 length 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
Here is line 34
harmonicmean=(5)/((1/div11)+(1/div22)+(1/div33)+(1/div44)+(1/div55))
 
  • #7
NicolasPan said:
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.
 

1. How can I extract individual digits from a number in Fortran?

The best way to break a number into separate digits in Fortran is to use the MOD and INT functions. The MOD function returns the remainder when one number is divided by another, while the INT function returns the integer part of a number. By repeatedly using these functions, you can extract each individual digit from a number.

2. Can I break a number into digits without using loops in Fortran?

Yes, you can use the MOD and INT functions in a single statement to break a number into digits without using loops. For example, digits = MOD(number, 10) will return the last digit of the number, and number = INT(number/10) will remove the last digit from the number, allowing you to extract the remaining digits in subsequent statements.

3. How can I handle decimal numbers when breaking them into digits?

When breaking a decimal number into digits, you can use the FLOOR function to truncate the number to an integer before using the MOD and INT functions. For example, digits = MOD(FLOOR(number), 10) will extract the last digit of the decimal number.

4. Is there a function specifically for breaking numbers into digits in Fortran?

Fortran does not have a built-in function for breaking numbers into digits. However, you can create your own function using the MOD and INT functions as mentioned above. Alternatively, there are some user-defined functions available online that you can use for this purpose.

5. Can I break a negative number into digits in Fortran?

Yes, you can break a negative number into digits in Fortran by using the ABS function to get the absolute value of the number before using the MOD and INT functions. For example, digits = MOD(ABS(number), 10) will extract the last digit of the negative number.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
587
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top