Question: I'm creating a program in which user enter mcqs and option and then save the data into the `txt` file.How can i do this .I try but it's not working.

```
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    ofstream outputFile;
    std::string fname;
    cout<<"Enter file name";
    std::getline (std::cin,fname);
    outputFile.open(fname+".txt");
    std::string name;
    std::string mcqs;
    int x;
    cout<<"How many questions want \n";
    cin>>x;

    for(int i=1;i<=x;i=i+1){
        cout<"Enter question "+i;
        std::getline (std::cin,name);
        outputFile << name << endl;
        cout<<"Option A";
        std::getline (std::cin,mcqs);
        outputFile << mcqs << endl;
        cout<<"Option B";
        std::getline (std::cin,mcqs);
        outputFile << mcqs << endl;
        cout<<"Option C";
        std::getline (std::cin,mcqs);
        outputFile << mcqs << endl;
        cout<<"Option D";
        std::getline (std::cin,mcqs);
        outputFile << mcqs << endl;
    }

    outputFile.close();
    cout << "Done!\n";
    getchar();
    return 0;
}

```

I want save `mcqs` and option in `txt` file.Thanks

Answer: 
[The relevant OEIS entry (A006995)](https://oeis.org/A006995) has a lot of nice tips if you read through it. For example, `a(2^n-1)=2^(2n-2)-1` lets you skip right to the (2n - 1)th palindrome really quickly.

It also gives several implementations. For example, the Smalltalk implementation works like this (note that the input value, `n`, starts with `1` for the first palindrome, `0`):

```
public static final int nthBooleanPalindrome(int n) {
    if (n == 1) return 0;
    if (n == 2) return 1;
    int m = 31 - Integer.numberOfLeadingZeros(n);
    int c = 1 << (m - 1);
    int b;
    if (n >= 3*c) {
        int a = n - 3*c;
        int d = 2*c*c;
        b = d + 1;
        int k2 = 1;
        for (int i = 1; i < m; i++) {
            k2 <<= 1;
            b += a*k2/c%2*(k2 + d/k2);
        }
    }
    else {
        int a = n - 2*c;
        int d = c*c;
        b = d + 1 + (n%2*c);
        int k2 = 1;
        for (int i = 1; i < m - 1; i++) {
            k2 <<= 1;
            b += a*k2/c%2*(k2 + d/k2);
        }
    }
    return b;
}

```
Idea to optimize,
Let's look at the palindrome sequence 0, 1, 11, 101, 111, 1001 etc...

All numbers must begin and end with 1, So the middle bits only changes and midle substring should be palindrome for full string to become palindrome, 

* So let's take a 2 digit binary number - one palindrome is possible.

The binary of the decimal **3** is a palindrome. **11**
* For a 3 digit binary number 2 palindromes are possible, **2\*(no of 1 digit palindrome)**

The binary of the decimal **5** is a palindrome. **101**

The binary of the decimal **7** is a palindrome. **111**
* For 5 digit binary number **4** palindromes are possible **2\*(no of 3 digit palindrome)**

**10001,10101, 11011, 11111**

and so on,

So it will be 2 + 20 + 21 + 22 +...... +2i-N , 

we solve for i and find out the palindrome number.

So by analysing this sequence we get an equation like **2(i/2)+1 -1 = N** 

where N is the No of palindrome, 

and i is the number of bits in the nth palindrome string, 

using this we can find the length of the String, from this we can find the string early.

This might be complex, but helps in solving higher values of N quickly....