Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems getting division output

Status
Not open for further replies.

urufukiba

Programmer
Nov 1, 2009
2
US
i am making a program that calculates the fibonacci sequence to the 1476th number in the sequence, then getting the first digit of each and calculating the percentages of how often each digit (1-9) shows up as the first one.

i have all of this working except printing out the percentages. when i go to print them out, all i get is a line of asterisks(*) I can't quite figure out what the issue is

here is my code:

Code:
FUNCTION fibonacci(n) RESULT(fib_n)
    IMPLICIT NONE
    REAL*8,INTENT(IN) :: n
    REAL*8 :: fib_n
    REAL*8 :: prev,current,next
    INTEGER i
    IF(n == 0) THEN
        fib_n = 0;
    ELSE IF(n == 1.0) THEN
        fib_n = 1.0;
    ELSE IF(n == 2.0) THEN
        fib_n = 1.0;
    ELSE
        prev = 0.0
        current = 1.0
        DO i = 2.0,n
            next = prev + current
            prev = current
            current = next
        END DO
            fib_n = current
    ENDIF
END FUNCTION fibonacci

PROGRAM CalculateFib
    IMPLICIT NONE
    REAL*8 number
    REAL*8 fibnumber
    REAL :: p1=0, p2=0, p3=0, p4=0, p5=0, p6=0, p7=0, p8=0, p9=0
    REAL :: n1=0, n2=0, n3=0, n4=0, n5=0, n6=0, n7=0, n8=0, n9=0, count=0
    INTEGER :: num1=0, num2=0, num3=0, num4=0, num5=0, num6=0, num7=0, num8=0, num9=0
    CHARACTER(LEN=50) :: numberString
    CHARACTER(LEN=1) :: singleDigit
    INTERFACE
        FUNCTION fibonacci(n)
            REAL*8, INTENT(IN) :: n
            REAL*8 :: fibonacci
        END FUNCTION fibonacci
    END INTERFACE
    number = 1
    DO number=1,1476
        fibnumber = fibonacci(number)
        Write( numberString,*) fibnumber
        numberString = adjustl(numberString)
        singleDigit = numberString(1:1)
        count = count+1
        if(singleDigit.eq.'1') num1=num1+1
        if(singleDigit.eq.'2') num2=num2+1
        if(singleDigit.eq.'3') num3=num3+1
        if(singleDigit.eq.'4') num4=num4+1
        if(singleDigit.eq.'5') num5=num5+1
        if(singleDigit.eq.'6') num6=num6+1
        if(singleDigit.eq.'7') num7=num7+1
        if(singleDigit.eq.'8') num8=num8+1
        if(singleDigit.eq.'9') num9=num9+1
    END DO

    write(*,'("Number of 1s: ",i5)')num1
    write(*,'("Number of 2s: ",i5)')num2
    write(*,'("Number of 3s: ",i5)')num3
    write(*,'("Number of 4s: ",i5)')num4
    write(*,'("Number of 5s: ",i5)')num5
    write(*,'("Number of 6s: ",i5)')num6
    write(*,'("Number of 7s: ",i5)')num7
    write(*,'("Number of 8s: ",i5)')num8
    write(*,'("Number of 9s: ",i5)')num9
    
    p1 = REAL(num1)/REAL(count)
    p2 = REAL(num2)/REAL(count)
    p3 = REAL(num3)/REAL(count)
    p4 = REAL(num4)/REAL(count)
    p5 = REAL(num5)/REAL(count)
    p6 = REAL(num6)/REAL(count)
    p7 = REAL(num7)/REAL(count)
    p8 = REAL(num8)/REAL(count)
    p9 = REAL(num9)/REAL(count)

    print ' '
    write(*,'("Percentage of 1s: ",i5)')p1
    write(*,'("Percentage of 2s: ",i5)')p2
    write(*,'("Percentage of 3s: ",i5)')p3
    write(*,'("Percentage of 4s: ",i5)')p4
    write(*,'("Percentage of 5s: ",i5)')p5
    write(*,'("Percentage of 6s: ",i5)')p6
    write(*,'("Percentage of 7s: ",i5)')p7
    write(*,'("Percentage of 8s: ",i5)')p8
    write(*,'("Percentage of 9s: ",i5)')p9
    
    

    pause 'Press Enter'
END PROGRAM CalculateFib
 
i actually solved this a couple of minutes after i posted this. i needed to use the 10F5.2 for formatting the write instead of the i5 that i had
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top