Write a C++ program to get the Excel column title that corresponds to a given column number (integer value)

  • برمجة سي بلس بلس
  • برمجة

Write a C++ program to get the Excel column title that corresponds to a given column number (integer value)

For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB

Sample Output:

Column Number n =  2 Excel column title = B

Column Number n =  29 Excel column title = AC

Column Number n =  153 Excel column title = EW

الأجوبة

#include <iostream>
using namespace std;

    string convert_num_to_Excel_Title(int num) {
        if (num == 0) {
            return "";
        }
        return convert_num_to_Excel_Title((num - 1) / 26) + static_cast<char>((num - 1) % 26 + 'A');
    }


int main(void)
{
    int n = 2;
    cout << "\nColumn Number n =  " << n << " Excel column title = " << convert_num_to_Excel_Title(n) << endl; 
    n = 29;
    cout << "\nColumn Number n =  " << n << " Excel column title = " << convert_num_to_Excel_Title(n) << endl; 
    n = 153;
    cout << "\nColumn Number n =  " << n << " Excel column title = " << convert_num_to_Excel_Title(n) << endl; 
    return 0;
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...