how to print a letter inside a series

hi everyone, my question is this:
how to print the following using C++ loop instructions

F12345
1F2345
12F345
123F45
1234F5
12345F
Last edited on
I've written the program but I won't give to you straight away. This isn't a homework site. Have you tried something on your own? If you did post it here.
yes i have
but all failed terribly

here is the one I'm working on

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream.h>
#include <conio.h>

int main(){
     int i,j;
     for(i=1;i<=5;i++){
              cout<<i;
              for(j=1;j<=5;j++){
              if(j==i) cout<<"F";
              else break;
              }
     }

getch();
return 0;
}



after excution it gives me only 1F2345
how to fix this, please
Last edited on
I offer the following politely, and in a spirit of helpfulness -

1. When posting code, put it between code tags.

2. Choose meaningful subject headings. "Help," is not a good example of this.
Here it is. I think there could be a simpler way of doing this, but this is how I did it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>

using namespace std;

int main(){
int positionF = 1;    //variable for where the F will be printed each time.
for (int i=1; i<=6; i++){            //this runs 6 times, each for every line
    bool printedF = false;          //this is to check for each line if the F has been printed or not
    for (int j=1; j<=5; j++){
        if (positionF==j && printedF==false){
            cout<<'F';                                    //output F if it's in the right place and has not been printed before.
            j--;           //j-- because we need to output all numbers from 1-5, if this wasn't we would skip a step.
            positionF++;  //increase positionF
            printedF = true;      //printedF to true so it won't be outputted again.
        }
        else{
            cout<<j;   //if it's not F's time to be output, output j.
        }
    }
    if (i==6){
        cout<<'F';
    }
    cout<<"\n";
}
}
thank you so much sir
this will help me alot
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
   char c[] = "F12345";

   for (int i = 0; i < 6; i++)
   {
      if (i > 0)
      {
         char t = c[i];
         c[i] = c[i - 1];
         c[i - 1] = t;
      }
      std::cout << c << "\n";
   }
}


F12345
1F2345
12F345
123F45
1234F5
12345F
thanks FurryGuy

anothor solution with less steps, that is very nice of u
thanks again
closed account (E0p9LyTq)
An alternate version without the if statement in the loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
   char c[] = "F12345";

   std::cout << c << "\n";

   for (int i = 1; i < 6; i++)
   {
      char t = c[i];
      c[i] = c[i - 1];
      c[i - 1] = t;
      std::cout << c << "\n";
   }
}
Topic archived. No new replies allowed.