Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:09:42+00:00 2026-06-14T02:09:42+00:00

I’m trying to write a simple Fortran code, for practicing. It is supposed to

  • 0

I’m trying to write a simple Fortran code, for practicing. It is supposed to multiply numbers in a range. Each time, the resulting product is converted into a string because I want to see if it consists of the same digits.

I tested the way I transform an integer into a string and typed the components of the string, and everything was going correctly. Then, I need to compare the components of the string, for which I use string(number:number). But I couldn’t get the code to do this correctly.

Here’s the code and the output:

program test
implicit none
character(10) myString
character(1) a,b,c,d,e,f
integer::i,j,k
do i=900,901,1
        j=900
        k=i*j
        write(*,*)'k =', k
        write(myString,'(i10)') k
        write(*,*)'myString = ', myString
        a=myString(1:1)
        b=myString(2:2)
        c=myString(3:3)
        d=myString(4:4)
        e=myString(5:5)
        f=myString(6:6)
        print*,a,b,c,d,e,f
        if (d==f) then
            print*,'hobla'
        else
            print*,'fobla'
        end if
end do

stop
end program test

So I defined characters: a,b,c,d,e,f to contain the components of the string. And used myString(i:i) to locate each component and store it in one of the characters a,b,c,d,e,f.
But it seems only the first two are working correctly, the rest is not being stored!

Output:

 k =      810000
 myString =     810000
     81
 fobla
 k =      810900
 myString =     810900
     81
 fobla

Notice 81. This was supposed to give 810000 the first time, and print “hobla”. And give 810900 the second time and print “fobla”. But this didn’t happen!

Can anybody show me how to let myString accept the zeros as characters?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:09:43+00:00Added an answer on June 14, 2026 at 2:09 am

    Okay, so I figured out the problem. Consider the following modifications to your code:

    program test
    implicit none
    character(10) myString
    character(1) a,b,c,d,e,f
    integer::i,j,k
    do i=900,901,1
       j=900
       k=i*j
       write(*,*)'k =', k
       write(myString,'(i10)') k
       write(*,*)'myString = ', myString
       a=myString(1:1)
       b=myString(2:2)
       c=myString(3:3)
       d=myString(4:4)
       e=myString(5:5)
       f=myString(6:6)
       write(*,*) a
       write(*,*) b
       write(*,*) c
       write(*,*) d
       write(*,*) e
       write(*,*) f
       if (d==f) then
           print*,'hobla'
       else
           print*,'fobla'
       end if
    end do
    
    stop
    end program test
    

    The resulting output is:

     k =      810000
     myString =     810000
    
    
    
    
     8
     1
     fobla
     k =      810900
     myString =     810900
    
    
    
    
     8
     1
     fobla
    

    This happens because the I format right-justifies numbers when printed. So there is a bunch of leading white-spaces because your number is only 6 digits while your string you are writing into is 10. So you get 4 leading spaces.

    If you change your format string so you have:

    write(myString,'(i10.10)') k
    

    then instead of padding with spaces it will pad with 0’s. That way you can always have digits to compare if you would rather.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.