In: Computer Science
Write a program in C that takes as input a four-digit hexadecimal number and prints the next
10 hexadecimal numbers. Define a hexadecimal number as
int hexNum[4]
Allow upper- or lowercase letters for input and use uppercase letters for the hexadecimal
output. For example, 3C6f should be valid input and should produce output 3C6F, 3C70,
3C71, . . . .
If you have any doubts, please give me comment...
#include<stdio.h>
#include<math.h>
#include<ctype.h>
int convertToDecimal(char hex[]){
int i=0, n=4;
int decimal = 0;
while(hex[i]!='\0'){
hex[i] = toupper(hex[i]);
if(hex[i]>='A' && hex[i]<='F')
decimal += (hex[i]-'A'+10)*pow(16, n-i-1);
else
decimal += (hex[i]-'0')*pow(16, n-i-1);
i++;
}
return decimal;
}
void convertToHex(int n, char hex[]){
int i=4;
while(n!=0){
int r = n%16;
if(r>=10)
hex[i-1] = r+'A'-10;
else
hex[i-1] = r+'0';
n/=16;
i--;
}
hex[4] = '\0';
}
int main(){
char hexNumber[4];
printf("Enter your input: ");
scanf("%s", hexNumber);
int decimal = convertToDecimal(hexNumber);
char next[4];
printf("The next 10 hexadecimal numbers are: \n");
for(int i=0; i<10; i++){
convertToHex(decimal+i, next);
printf("%s\n", next);
}
return 0;
}