Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Factoring in Fortran

  1. Mar 19, 2008 #1
    Got to write a program that factors a given number(>1)

    Here is what I have:

    program Factors
    implicit none
    real::n, i

    print*, "enter a number:"
    read*, n

    i=2
    do while (n>1)
    if (mod(n, i)==0) then
    i=i+1
    n=n/i
    else
    write (*,*) ' X= ', X
    end if
    end do


    end program

    It works great but lets say you put in 600, it will give

    2
    2
    2
    3
    5
    5

    which is right but the teacher wants it to say:

    2**3*3*5**2

    I dont have a clue how to make it do that.

    Thanks for any help. :)
     
  2. jcsd
  3. Mar 19, 2008 #2

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

    You'll need an inner loop that counts how many times a given factor is used, storing that in another variable. If it's used once write it with a "*", otherwise write it with a "**", the exponent, then another "*".
     
  4. Mar 19, 2008 #3
    ok. I put this together but Im getting an error


    program Factors
    implicit none
    real::n, i, x
    integer::f, c=20

    print*, "enter a positive integer:"
    read*, n


    i=2
    do while (n>1)
    if (mod(n, i)==0) then
    i=i+1
    n=n/i
    else


    write(*, 100) i, "**", c
    100 format(I2, A2, I2, $)
    end if
    end do


    end program
     
  5. Mar 19, 2008 #4
    I know I just signed up but this thing is due in a few hours.

    Any help is much appreciated.
     
  6. Mar 20, 2008 #5
    well what's the error?
     
  7. Mar 20, 2008 #6
    It says there is an error in the (I2, A2, I2, $). It has an arrow pointing to the first I2.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Factoring in Fortran
  1. Fortran 90 (Replies: 1)

  2. FORTRAN Help (Replies: 1)

  3. Fortran Help (Replies: 2)

  4. Infinity in fortran (Replies: 5)

  5. Linking Fortran (Replies: 5)

Loading...