In: Computer Science
I have below code! I would like to to convert the larger variable to hex and get len() of larger once it's in hex format.
Example: if larger number is 900000 then the hex is DBBA0 then len of DBBA0 is 5. I need everything save in new variable!
It's in C.
// Function to find largest element
int largest(int a[], int n)
{
int large = a[0], i;
for(i = 1; i < n; i++)
{
if(large < a[i])
large = a[i];
}
return large;
}
Program :
#include <stdio.h>
void largest(int a[],int);
void hexadecimal(int);
int main()
{
int i,n,a[100];
printf("How values do wwant to enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
largest(a,n);
return 0;
}
void largest(int a[], int n)
{
int large = a[0], i;
for(i = 1; i < n; i++)
{
if(large < a[i])
large = a[i];
}
printf("\nThe largest value is:%d",large);
decToHexa(large);
}
void decToHexa(int n)
{
char hex[100];
int i = 0,c=0;
while(n!=0)
{
int t = 0;
t = n % 16;
if(t < 10)
{
hex[i] = t + 48;
i++;
}
else
{
hex[i] = t + 55;
i++;
}
n = n/16;
}
printf("\nhexadecimal value is:");
for(int j=i-1; j>=0; j--)
{
printf("%c", hex[j]);
c++;
}
printf("\nThe length of hexa decimal vlaue is : %d",c);
}
out put of the program :