{"id":26136,"date":"2017-10-26T20:02:14","date_gmt":"2017-10-26T14:32:14","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26136"},"modified":"2017-10-26T20:02:14","modified_gmt":"2017-10-26T14:32:14","slug":"c-programming-excel-column-name","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-excel-column-name\/","title":{"rendered":"C++ Programming &#8211; Excel column name"},"content":{"rendered":"<p>MS Excel columns has a pattern like A, B, C, \u2026 ,Z, AA, AB, AC,\u2026. ,AZ, BA, BB, \u2026 ZZ, AAA, AAB \u2026.. etc. In other words, column 1 is named as \u201cA\u201d, column 2 as \u201cB\u201d, column 27 as \u201cAA\u201d.<span id=\"more-128620\"><\/span><\/p>\n<p>Given a column number, find its corresponding Excel column name. Following are more examples.<\/p>\n<pre>Input          Output\r\n 26             Z\r\n 51             AY\r\n 52             AZ\r\n 80             CB\r\n 676            YZ\r\n 702            ZZ\r\n 705            AAC<\/pre>\n<p>Suppose we have a number n, let\u2019s say 28. so corresponding to it we need to print the column name. We need to take remainder with 26.<\/p>\n<p>If remainder with 26 comes out to be 0 (meaning 26, 52 and so on) then we put \u2018Z\u2019 in the output string and new n becomes n\/26 -1 because here we are considering 26 to be \u2018Z\u2019 while in actual it\u2019s 25th with respect to \u2018A\u2019.<\/p>\n<p>Similarly if the remainder comes out to be non zero. (like 1, 2, 3 and so on) then we need to just insert the char accordingly in the string and do n = n\/26.<\/p>\n<p>Finally we reverse the string and print.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Example:<\/strong><br \/>\nn = 700<\/p>\n<p>Remainder (n%26) is 24. So we put \u2018X\u2019 in output string and n becomes n\/26 which is 26.<\/p>\n<p>Remainder (26%26) is 0. So we put \u2018Z\u2019 in output string and n becomes n\/26 -1 which is 0.<\/p>\n<p>Following is C++ implementation of above approach.<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%23include%3Cbits%2Fstdc%2B%2B.h%3E%0A%23define%20MAX%2050%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Function%20to%20print%20Excel%20column%20name%20for%20a%20given%20column%20number%0Avoid%20printString(int%20n)%0A%7B%0A%20%20%20%20char%20str%5BMAX%5D%3B%20%20%2F%2F%20To%20store%20result%20(Excel%20column%20name)%0A%20%20%20%20int%20i%20%3D%200%3B%20%20%2F%2F%20To%20store%20current%20index%20in%20str%20which%20is%20result%0A%20%0A%20%20%20%20while%20(n%3E0)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Find%20remainder%0A%20%20%20%20%20%20%20%20int%20rem%20%3D%20n%2526%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20remainder%20is%200%2C%20then%20a%20\u2019Z\u2019%20must%20be%20there%20in%20output%0A%20%20%20%20%20%20%20%20if%20(rem%3D%3D0)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20str%5Bi%2B%2B%5D%20%3D%20\u2019Z\u2019%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%20(n%2F26)-1%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%20%2F%2F%20If%20remainder%20is%20non-zero%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20str%5Bi%2B%2B%5D%20%3D%20(rem-1)%20%2B%20\u2019A\u2019%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%20n%2F26%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20str%5Bi%5D%20%3D%20\u2019%5C0\u2019%3B%0A%20%0A%20%20%20%20%2F%2F%20Reverse%20the%20string%20and%20print%20result%0A%20%20%20%20reverse(str%2C%20str%20%2B%20strlen(str))%3B%0A%20%20%20%20cout%20%3C%3C%20str%20%3C%3C%20endl%3B%0A%20%0A%20%20%20%20return%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20printString(26)%3B%0A%20%20%20%20printString(51)%3B%0A%20%20%20%20printString(52)%3B%0A%20%20%20%20printString(80)%3B%0A%20%20%20%20printString(676)%3B%0A%20%20%20%20printString(702)%3B%0A%20%20%20%20printString(705)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC++\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output<\/strong><\/p>\n<pre>Z\r\nAY\r\nAZ\r\nCB\r\nYZ\r\nZZ\r\nAAC<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming &#8211; Excel column name &#8211; Mathematical Algorithms &#8211; MS Excel columns has a pattern like A, B, C, \u2026 ,Z, AA, AB, AC,\u2026. ,AZ, BA, BB, \u2026 ZZ, AAA, AAB <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83515,1,74058],"tags":[77266,77296,77259,77281,77251,77276,77269,77268,77277,77286,77287,77257,77253,77284,77274,77288,77270,77265,77272,77254,77255,77256,77299,77292,77280,77278,77282,77271,77262,77298,77252,77297,77279,77289,77294,77275,77261,77283,77295,77273,77285,77291,77263,77264,77260,77290,77258,77267,77293,77300],"class_list":["post-26136","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-mathematical-algorithms","tag-a-range-name","tag-apply-names-excel","tag-cells-names-list","tag-column-cell","tag-column-excel","tag-convert-letters-to-numbers-excel","tag-convert-text-to-excel-columns","tag-define-excel","tag-define-name","tag-definition-of-cell-in-excel","tag-excel-cell-number","tag-excel-column-and-row","tag-excel-definition","tag-excel-find-in-column","tag-excel-formula-name","tag-excel-list-of-names","tag-excel-name","tag-excel-name-box","tag-excel-name-manager","tag-excel-row-and-column","tag-excel-row-and-column-numbers","tag-excel-row-to-column","tag-excel-sheet-column-number","tag-excel-sheet-name-formula","tag-excel-turn-columns-into-rows","tag-excel-worksheet-columns","tag-formula-column","tag-how-to-create-a-range-in-excel","tag-lookup-in-excel","tag-microsoft-excel-column","tag-ms-excel-rows-and-columns","tag-name-a-range","tag-name-a-range-in-excel-2013","tag-name-box-definition","tag-name-cell","tag-name-definition","tag-name-excel","tag-name-list-excel","tag-name-of-formula","tag-name-to-formula","tag-named-range","tag-names-of-cells","tag-naming-formulas","tag-range-formula","tag-row-and-column","tag-row-column-cell","tag-row-excel","tag-row-to-column-excel","tag-spreadsheet-rows-and-columns","tag-the-name-box"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26136"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26136\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}