-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion10.c
85 lines (76 loc) · 1.86 KB
/
question10.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Find excel column name for given excel column number
Excel column number and name relation has to do with the number system. In case of excel the number system
base is 26. Therefore numbers are from 1-26. Just that numbers in this case start from 1 and not from zero
as compared to other bases for eg 2 which has 0 and 1 and so on.
Therefore given a number we can keep dividing it by 26 until we receive a number which is less than 26 as
remainder.
Then we take the remainder and quotient from bottom to top and assign them alphabets corresponding to their
values.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX 50
int GetPower(int number, int exponent)
{
int power = 1;
for(int i=0; i<exponent; i++)
power *= number;
return power;
}
void calculateNumber(char *str){
// printf("INSIDE FUNCTION %s\n", str);
int i;
int length = strlen(str);
int sum = 0;
// printf("sum value now %d\n", sum);
int exp = 0;
for(i=length-1; i>=0;i--){
sum += (str[i] - 'A' + 1) * (GetPower(26, exp));
// printf("num value now is %d\n", sum);
exp++;
}
printf("number is %d\n", sum);
}
void calculateName(int num){
if(num <= 26){
printf("%c\n",'A'+num-1);
return;
}
char str[MAX];
int z = 0;
while(num > 0){
int rem = num%26;
if(rem == 0){
str[z++]='Z';
num = (num/26)-1;
}else{
str[z++] = 'A' + rem - 1;
num = num/26;
}
}
strrev(str);
printf("%s\n", str);
}
int main(){
int num, step;
char word[256];
while(1){
printf("1. convert excel column number to name\n");
printf("2. convert excel name to column number\n");
scanf("%d",&step);
switch(step){
case 1: printf("enter the excel column number\n");
scanf("%d",&num);
calculateName(num);
break;
case 2: printf("Enter string for column name\n");
scanf("%s",word);
calculateNumber(word);
break;
}
}
return 0;
}