Tuesday, 5 August 2014

Conversion of Number

Number Conversion

Decimal to Binary Conversion

Decimal number have base 10 and Binary number have base 2.
/*******************************************************
	           Decimal to Binary Conversion
********************************************************/
#include< stdio.h>
#include< conio.h>
void main()
{
int no,rem[20],i=0,j;
clrscr();
printf("Enter any num : ");
scanf("%d",&no);
while(no>0)
{
rem[i]=no%2;
i++;
no=no/2;
}
printf("Binary Number is :");
for(j=i-1;j>=0;j--)
{
printf(" %d",rem[j]);
}
getch();
}
Output
Enter any number : 7
Binary number is : 111

Decimal to OctalConversion

Decimal number have base 10 and Octalnumber have base 8.
/*****************************************************
	   Decimal to Octal Conversion
******************************************************/
#include< stdio.h>
#include< conio.h>
void main()
{
int no,rem[20],i=0,j;
clrscr();
printf("Enter any num : ");
scanf("%d",&no);
while(no > 0)
{
rem[i]=no%8;
i++;
no=no/8;
}
printf("Binary Number is :");
for(j = i - 1; j > = 0 ; j--)
{
printf(" %d",rem[j]);
}
getch();
}
Output
Enter any number : 23
Binary number is : 27

Decimal to Hexadecimal Conversion

Decimal number have base 10 and Hexadecimal number have base 16.
/*****************************************************
	   Decimal to Hexa Conversion
******************************************************/
#include< stdio.h>
#include< conio.h>

void main()
{
int num,rem[20],hex=0,i=0,j;
clrscr();
printf("\n Enter any Number  : ");
scanf("%d",&num);
while(num > 0)
  {
    rem[i] = num % 16;
    num = num / 16;
    i++;
  }
printf("Hexadecimal number : ");
for(j = i - 1 ; j > = 0 ; j--)
  {
    switch(rem[j])
    {
      case 10:
	  printf("A");
	  break;
      case 11:
	  printf("B");
	  break;
      case 12:
	  printf("C");
	  break;
      case 13:
	  printf("D");
	  break;
      case 14:
	  printf("E");
	  break;
      case 15:
	  printf("F");
	  break;
      default :
	 printf("%d",rem[j]);
    }
  }
getch();
}
Output
Enter any number : 231
Hexadecimal number : E7

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...