How To Add Hexadecimal Numbers In C
Introduction to Hexadecimal in C
In C programming language, a hexadecimal number is a value having a made upwards of 16 symbols which have ten standard numerical systems from 0 to 9 and 6 extra symbols from A to F. In C, the hexadecimal number system is also known as base-xvi number system. In C there is no data blazon to shop hexadecimal values similar float or long or double instead y'all can shop in the integral type of data types. In C programming language, hexadecimal value is represented every bit 0x or 0X and to input hexadecimal value using scanf which has format specifiers like %x or %Ten.
Functions of Hexadecimal in C Programming
In this article, we are discussing hexadecimal value in the C programming linguistic communication. Hexadecimal is also similar an integral value that has no split up data type. We already know that there are sixteen symbols for hexadecimal values like 0, 1, 2, 3, 4, five, 6, vii, 8, ix, A, B, C, D, E, F. Hither A, B, C, D, C, E, F represents 11, 12, thirteen, 14, 15. Let us see an example and its syntax:
Syntax:
Scanf ("%10", &var_name);
To convert decimal number to hexadecimal number we take few steps to follow:
- Firstly dissever the given decimal number by 16. (Consider it as integer sectionalisation).
- Note down the residue of the above division of decimal numbers.
- Then divide this remainder by 16. Continue until you lot get the event every bit 0. (Consider partition as integer division).
- So the hexadecimal value obtained volition be the sequence of digits of the remainder from last to start.
Examples of Hexadecimal in C
Allow us try to catechumen a decimal number 590 to hexadecimal value using the higher up steps:
- Divide 590 by 16 i.e. 590 / 16 result = 36 and balance =fourteen ( East is hexadecimal value of 14).
- Split up the obtained result by 16 in the above step, so 36 / 16 effect = 2 and remainder = 4 (four is decimal value).
- Divide the obtained outcome by xvi in the above step, so 2 / sixteen event = 0 and remainder =2 ( ii every bit decimal value).
- Then the hexadecimal value of decimal number 590 is the sequence of digits of the residuum from last to first which will be 24E.
Example #1
Now let u.s.a. see the program in C programming for converting decimal number to hexadecimal number:
Code:
#include<stdio.h>
int main() {
long int decNum,rem,quo;
int i=1,j,temp;
char hexadecNum[100];
printf("Enter whatsoever decimal number to convert it to hexadecimal Number: ");
scanf("%ld",&decNum);
quo = decNum;
while(quo!=0)
{
temp = quo % 16;
if( temp < x)
temp =temp + 48; else
temp = temp + 55;
hexadecNum[i++]= temp;
quo = quo / 16;
}
printf("hexadecimal value of decimal number entered is %d: ",decNum);
for (j = i -1 ;j> 0;j--)
printf("%c",hexadecNum[j]);
return 0;
}
Output:
In the higher up instance, we are press decimal number 590 to its hexadecimal number as 24E.
We can even convert hexadecimal number to decimal number also that is to extract any digit from a hexadecimal number we have to multiply the number with base of operations 16 and and so add it to the decimal value. Let us consider an case beneath to meet how we tin can extract decimal numbers from a hexadecimal number.
Example:
Hexadecimal number = 1AB
Equally discussed above we saw A represents 10 and B represents xi. Now we volition multiply with proper base with power of 16. So
1 = 1 * (16^2) = 256
A = ten * (16^1) = 160
B = eleven * (16^0) = 11
And then at present we have to add all these three results to obtain decimal value.
256 + 160 + 11 = 427
Therefore the decimal value for hexadecimal 1AB is 427.
Case #2
Below is the programme for converting given a hexadecimal number to decimal number:
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int decnum=0, rem, i=0, len=0;
char hexdecnum[twenty];
printf("Enter any Hexadecimal Number to convert it to decimal number: ");
scanf("%s", hexdecnum);
while(hexdecnum[i]!='\0')
{
len++;
i++;
}
len--;
i=0;
while(len>=0)
{
rem = hexdecnum[len];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=90)
rem = rem-55;
else
{
printf("\north Invalid Hexadecimal digit");
getch();
render 0;
}
decnum = decnum + (rem*pow(sixteen, i));
len--;
i++;
}
printf("\nDecimal Value of entered Hexadecimal number = %d", decnum);
getch();
return 0;
}
Output:
Enter any Hexadecimal Number to convert it to decimal number: 1AB
Decimal Value of entered Hexadecimal number = 427
In the above plan, we are converting a hexadecimal number 1AB to a decimal number as 427.
Recommended Manufactures
This is a guide to Hexadecimal in C. Hither we talk over the Introduction to Hexadecimal in C and its Working besides every bit Examples forth with its Code Implementation. You lot can likewise become through our other suggested articles to learn more than –
- Prime Numbers in C | Examples
- How to Opposite Number in C?
- Introduction to Contrary Cord in C
- Prime Numbers in Java | Summit iii Examples
How To Add Hexadecimal Numbers In C,
Source: https://www.educba.com/hexadecimal-in-c/
Posted by: thompsonfalwye.blogspot.com
0 Response to "How To Add Hexadecimal Numbers In C"
Post a Comment