Excel Puzzle

Given a number, convert it into excel row/column format. For example, 0 is A, 26 is AA, 701 is ZZ, 702 is AAA etc.

I came up with the following C++ code:

 #include <iostream>
#include <cstdio>
#include <string>

using std::string;
using std::cout;
using std::cin;
using std::endl;

void printit(int number) {

int res;

if (number/26) {
printit((number/26) – 1);
}

res = number % 26;
printf(“%c”, res+65);
}

// Program to print a given number in excel row/column format.
int main() {

while(1) {
cout << “Enter a number: “;
int number;
cin >> number;

if (!cin) {
return 1;
}

printit(number);
cout <<endl;
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s