Saturday, 27 September 2014

Program in c with octal and hexadecimal numbers.

When we study c language then we don't notice octal number and hexadecimal number . But c language Provides the facility so we can work on octal or decimal. let see a example.

Example :- 

                         #include<stdio.h>
                         Void main()
                         {
                         Printf(“%d %d %d %d \n”,72,072,0x72,0X72);
                         }

Output = 72,58,114,114


Explanation :-

Here is to show the value of 3 types,
1st Decimal (72)
2nd octal (072)
And 3rd hexadecimal (0x72,0X72)
1st Decimal - here is %d means decimal and 72 also in  Decimal therefore also  output will remain decimal and will print 72.
2nd octal - Here %d means decimal But we put 0 before any constant no. then this not in decimal he will remain change in octal  like 072.
But in our program we use %d so this octal no (072) is change in decimal and our output become in decimal 58……
072= 0*8^2+7*8^1+2*8^0=58
3rd hexadecimal - Here% d means decimal But we put 0x,0X before any constant no. then this not in decimal he will remain change in hexadecimal  like 0x72,0X72.
But in our program we use %d so this hexadecimal no (0x72, 0X72) is change in decimal and our output become in decimal 114……
0x72= 0*16^2+7*16^1+2*16^0=114



Again wee see with a program how to change decimal to octal and hexadecimal.

#include<stdio.h>
Void main()
{
Printff(“%d,%o %x”,72,725,72);

}

Output :- 72,110,48
1st Decimal (%d)
2nd octal (%o)
And 3rd hexadecimal (%x)

1st Decimal - Here %d indicate decimal no. and 72 also a decimal no so our output also remain decimal 72.

2nd octal - Here %o indicate octal no. and 72 is  a decimal no so our output also remain change in decimal to octal so output=110.
72 =
As we know conversion rule between decimal to octal..
8
72
0
8
9
1

1

So value is 110
3rd hexadecimal Here %x indicate hexadecimal no. and 72 is  a decimal no so our output also remain  change in decimal to hexadecimal so output=48.
72=
As we know conversion rule between decimal to hexa decimal;
16
72
8

4

So output = 48.















No comments:

Post a Comment